Cancel down 2 integers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cancel down 2 integers
# 1  
Old 01-05-2012
[Solved] 'Cancel down'/reduce 2 integers

Wonderful evening to all of you!

My problem has to possible starting points.
Well, not really, but getting to either one is no problem at all.
So i got either a string in the format of "1920x1080" or simply the integers X = 1920 and Y = 1080.

When I am done, I would like to have an output as the following "16x09" or at least X = 16 and Y = 9, which of course could be simply concatenated.

As I am fairly new to shell-scripting, I got no idea how to even begin, and neither google nor the search were of help to me..

Last edited by jakunar; 01-05-2012 at 10:31 PM..
# 2  
Old 01-05-2012
Code:
echo "1920x1080" | awk -F"x" ' { print "X = " $1 " and Y = " $2 } '

Is this what you want?
# 3  
Old 01-05-2012
Nope not exactly, but never the less thank you for trying :-)

Hm english is not my native language and I don't know wheter i used the right word for the meaning I intended to express..

What i meant by "to cancel down" is for example: 6/4 => 3/2
Maybe reducing would be a more fitting expression?
# 4  
Old 01-05-2012
Try Perl.

Code:
$ perl -MMath::BigInt=bgcd -le '$x=bgcd(1920,1080); print 1920/$x." ".1080/$x'
16 9

This User Gave Thanks to balajesuri For This Post:
# 5  
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
# 6  
Old 01-06-2012
A humble bash code.

bash code:
  1. #! /bin/bash
  2. y=$1; r=$2
  3. while &#91; $r -ne 0 ]
  4. do
  5.     gcd=$r
  6.     r=$(($y % $r))
  7.     y=$gcd
  8. done
  9. echo $(($1 / $gcd))
  10. echo $(($2 / $gcd))

Code:
$ ./test.sh 1024 768
4
3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question