A Math problem using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A Math problem using shell script
# 1  
Old 09-08-2011
A Math problem using shell script

Have a bit complicated math query ..

Basically i am given a number which is > 50 ..
I am suppose to find the calculation to get a number which is equal or more than the input number and is also a multiple of any number between 20 - 30 .

Code:
 
For example .
Input number is 60 .
Now 20x3 =60   and  30x2=60   , so my ouput should be 20x3=60,30x2=60
 
2nd example 
Input number is 59
In this case the next number possible to 59 is 60 using the logic above ,so
my output would again be  20x3=60,30x2=60.
 
3rd example 
Input number is 113
In this case the next number which fits the criteria would be 116 .
29x4=116   , so my output should be 29x4=116

Again ... the key is that the number has to be a multiple of any number between 20 -30 and should be equal to or next highest number to the input number ....
I have to use the logic as part of a bigger script ...


Thanks
# 2  
Old 09-08-2011
This should work fine in sh/ksh/bash:

Code:
my_query()
{
    VALUE=$1
    while true
    do
       DIV=20
       while [ $DIV -le 30 ]
       do
          let FACTOR=VALUE/DIV
          let RESULT=FACTOR*DIV
          [ $RESULT -eq $VALUE ] && answer=$answer","${DIV}x${FACTOR}=$VALUE
          let DIV=DIV+1
       done
       if [ -n "$answer" ]
       then
          # Remove leading comma
          echo $answer | sed 's/^,//'
          return
       fi
       let VALUE=VALUE+1
    done
}
printf "Number: "
read number
my_query $number


Edit: Oh, BTW for your last example 23x5=115
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 09-09-2011
Code:
$ cat query
awk -v num=$1 'BEGIN{while (1) {for (i=20;i<=30;i++) if (num%i=="0") {printf "%d X %d = %d\n", i, num/i, num;exit} ; num++}}'

$ ./query 131
22 X 6 = 132

$ ./query 113
23 X 5 = 115

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for solving the math question

Can such Puzzle solve through UNIX script? if yes, what could be the code? This has been solve in C language. we were trying to solve this through shell but could not because of not able to pass 1st argument with multiple value. we are not expert in unix scripting. Below is the puzzle John is a... (4 Replies)
Discussion started by: anshu ranjan
4 Replies

2. UNIX for Dummies Questions & Answers

Variables and math in Old skool Bourne Shell

Hey everybody, I've been searching google and these forums and have found some solutions to the issues I've been having today within the OLD Bourne Shell. I am following chapter 6 of the Guide to Unix using Linux 4th Edition. I am working on some basic calculations with variables in the BASH... (3 Replies)
Discussion started by: mr.rhtuner
3 Replies

3. Shell Programming and Scripting

Math calculation over shell

Hi I am trying to calculate the rate at which something is happening. I have 2 files- a1 and b1. I want to calculate something like this ((wc -l a1)/(wc -l a1 + wc -l b1))*100 over a loop for different a and b. Is this possible, help me out fellas. Thanks a lot :) (5 Replies)
Discussion started by: jamie_123
5 Replies

4. Shell Programming and Scripting

Shell script newbie, what is problem with my script?

Hello, Ubuntu server 11.10 can anybody help what is problem with my shell script? #!/bin/bash #script to find out currently logged on user is root or not. if ] then echo "You are super" else echo "You are awesome!" fi When I run script, I get following output ./uid: line 3: I... (4 Replies)
Discussion started by: kaustubh
4 Replies

5. Shell Programming and Scripting

problem in shell script

hi every body this is my first thread in this forum, i hope find a solution for my problem i have to write a script bt i still have some error and i don't know how to correct them $ for i in `seq 500 505`; do ./generateur_tache $i tache$i.txt; nprocs=$i; copt$i=`cat tache$i.txt | ./copt.awk` ;... (10 Replies)
Discussion started by: ordo_ordo
10 Replies

6. Shell Programming and Scripting

Shell script problem

i have a set of commands to be executed, which is working perfectly when executed from terminal line by line but when its put in a shell script its not working i.e its giving FileNotFoundException. how do i resolve it my code is like #!/bin/sh pushd /Desktop/Solris_Installer/ROCore jar xf... (4 Replies)
Discussion started by: divyakprabh
4 Replies

7. Shell Programming and Scripting

Script math calculation

Hi Gurus, I'm currently using HP-UX B.11.23. I've a simple calculation script which performs the task below. -> echo "240021344 / 1024 /1024" | bc Output: 228 240021344 is KB value. When I tried to perform the same calculate in Ms Excel, it produces a different result: 228.9021912.... (12 Replies)
Discussion started by: superHonda123
12 Replies

8. Shell Programming and Scripting

call shell script from perl cgi script problem

hi,, i have perl scipt with line : system('./try.sh $t $d $m'); in shell scipt try.sh i have the line: echo $1 its not printing value of $t that i hav passed..y is it so..i am running it from apache web server (2 Replies)
Discussion started by: raksha.s
2 Replies

9. UNIX for Dummies Questions & Answers

Can Any help me with the math on this shell script?

Develop a grade calculating program. This program will process all students in the file. This program should neatly display each field of each student's record *and* adds the following items: Course Average and Letter Grade. The course average is calculated by the following weights: 50% for quiz... (7 Replies)
Discussion started by: knp808
7 Replies

10. UNIX for Dummies Questions & Answers

shell script problem

hi all. im using the awk command to search a file for a matching number. lets say the numbers in the file are 4588281, 4588282 and 4588283. my problem is when i search for 8, the three numbers above appear. I want to have it so that if i search for any number that is not matching EXACTLY... (2 Replies)
Discussion started by: djt0506
2 Replies
Login or Register to Ask a Question