Sponsored Content
Full Discussion: Cancel down 2 integers
Top Forums Shell Programming and Scripting Cancel down 2 integers Post 302587737 by jakunar on Thursday 5th of January 2012 09:26:04 PM
Old 01-05-2012
Thank you balajesuri!

I like you short perl version...

Meanwhile I tried to write something on my own and finally managed to do so..

It's not pretty and it's not optimized, but it works..
Would be nice, if s.o. could tell me, if I made any mistakes,
or if s.th. could/should be made differently..

Here is my bash-version using: comm, cut, factor, let, sed and tr

Code:
#!/bin/bash
## reduce2ints.sh

#
# returns the reduced values of 2 given integers.
#

# error-handling
if [[ ( "$#" -lt 2 ) ]]; then
  echo -e "Error!!!\x0ATry \"$0 value1 value2\"";
  exit 1;
else

# put parameters in own variables
  X=$1;  
  Y=$2;
  
# split values into their prime-factors and put them in arrays
  declare -a XPrimes=$(factor $X | cut -d$'\x20' -f2-);
  declare -a YPrimes=$(factor $Y | cut -d$'\x20' -f2-);
  
# subtract arrays from eachother to get the differences
  declare -a RestX=("$(comm -23 <(echo ${XPrimes} | sed 's/ /\n/g') <(echo ${YPrimes} | sed 's/ /\n/g') | tr "\n" " ")");
  declare -a RestY=("$(comm -13 <(echo ${XPrimes} | sed 's/ /\n/g') <(echo ${YPrimes} | sed 's/ /\n/g') | tr "\n" " ")");
  
# prepare outvariables
  NewX=1;
  NewY=1;

# calculate the 'rests'
  for element in ${RestX[@]}; do
    let NewX=$NewX*$element;
  done
  
  for element in ${RestY[@]}; do
    let NewY=$NewY*$element;
  done
  
# output the solutions
  echo "1. Reduced Value=$NewX";
  echo "2. Reduced Value=$NewY";
fi

# EOF

Good night and thanks again!
jakunar
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to cancel all printjobs at once?

Hi unix-friends, I've got an RS6000 with Unix 4.3 as OS. I'm writing a script that i want to use for cancelling all print jobs at once. Does anyone have any good idea's, hints, or tips? Hope to hear from you, Erik (3 Replies)
Discussion started by: Erik Rooijmans
3 Replies

2. UNIX for Advanced & Expert Users

cancel the massage

Hi all In Aix system Someone know i want to cancel the massage. if some user print something and user root deleting the job i got brodcast message " message from queueing system job number XXX has been deleteing from queue. <EOT> " I use the cancel command (1 Reply)
Discussion started by: goldfelda
1 Replies

3. UNIX for Dummies Questions & Answers

Cancel QCompile

I need help, please!!!!!!!! I have accidentally selected to compile all my programs, which will probably take forever!! How can I cancel the compiling of the programs. I see in qstatus the huge list of programs waiting to be compiled. Thanks! :eek: (1 Reply)
Discussion started by: itldp
1 Replies

4. Shell Programming and Scripting

integers in the if statement

hi, im trying to compare two variables in csh to put in an if statement, eg: set a = $firstnum set b = $secondnum if ($a -ge $b) echo $a But I get an error ("if: Expression syntax"). How can I make csh see my variables as integers? thanks in advance! (5 Replies)
Discussion started by: Deanne
5 Replies

5. Programming

Using write() with integers in C

I'm trying to write an integer to a file using the write() function, but write() requires the parameter to be written to be a const void*. How would I go about doing this? also: using itoa() produces a " warning: implicit declaration of function 'itoa' " even though i have #included stdlib.h (2 Replies)
Discussion started by: h@run
2 Replies

6. Post Here to Contact Site Administrators and Moderators

cancel account

id like to cancel my account please. thanks (5 Replies)
Discussion started by: samb057
5 Replies

7. Post Here to Contact Site Administrators and Moderators

Cancel Account

I accidentally typed a wrong user name... pls delete my account so that i can create a new one.. Thanks (4 Replies)
Discussion started by: Pat_Martin
4 Replies

8. UNIX for Dummies Questions & Answers

Cannot cancel a print

Hi Everyone, I am trying to cancel a print. I am logged on as the user of the print and when I use the command 'cancel print_job' I get the message 'print_job: not authorized' I have cancelled other prints in the queue, but this particular job in the queue cannot delete. I even logged on as... (5 Replies)
Discussion started by: Scarlet
5 Replies

9. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

10. Shell Programming and Scripting

Comparing Integers (I think)

Hi, I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is... (8 Replies)
Discussion started by: Rhysers
8 Replies
FACTOR(6)							 BSD Games Manual							 FACTOR(6)

NAME
factor -- factor a number SYNOPSIS
factor [number ...] DESCRIPTION
The factor utility factors integers larger than 1. When a number is factored, it is printed, followed by a ``:'', and the list of (prime) factors on a single line. Factors are listed in ascending order, and are preceded by a space. If a factor divides a value more than once, it will be printed more than once. When factor is invoked with one or more arguments, each argument will be factored. When factor is invoked with no arguments, factor reads numbers, one per line, from standard input, until end of file or error. Leading white-space and empty lines are ignored. Numbers may be preceded by a single +. Integer less than 2 are rejected. Numbers are terminated by a non-digit character (such as a newline). After a number is read, it is factored. Input lines must not be longer than LINE_MAX - 1 (currently 2047) characters. By default, factor is compiled against the OpenSSL bignum implementation openssl_bn(3), which lets it handle arbitrarily large values. (Note however that very large values can take a very long time to factor.) If factor is compiled without OpenSSL it is limited to the maximum value of unsigned long. DIAGNOSTICS
Out of range or invalid input results in an appropriate error message being written to standard error. BSD
May 15, 2010 BSD
All times are GMT -4. The time now is 08:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy