×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Count up several variables ...

Count up several variables ...

Count up several variables ...

(OP)
Hi folks,

sorry I must be getting a little bit rusty ... sad

Could you help me out with the following:

I have 5 Variables (A B C D E).
Each Variable is starting with a value of 1 and can count up to 2000.
I want to start with A=1 B=1 C=1 D=1 E=1 and first count up E up to 2000.
As soon as it reaches 2000 the value D shall be increased to 2 and E shall run up from 1 to 2000 again and so on until D also reaches 2000. If so C shall be increased to 2, D back to 1 and E count up from 1 to 2000 again ...

So what I want is to run all possible combinations where A B C D E can become something between 1 and 2000.

For each combination I want to perform some calculations and if the result I'm looking for (Which is: The combination where all my calculations will have the same result) is found the script shall stop and report me the current values for A B C D E ...

I started with the following lines but somehow I got stuck and my mind refuses to think any further even though it's not such a huge programming problem ... sad

CODE

#!/usr/bin/ksh

A=1
B=1
C=1
D=1
E=1


while [ $A -lt 2000 ];
do
 while [ $B -lt 2000 ];
 do
  while [ $C -lt 2000 ];
  do
   while [ $D -lt 2000 ];
   do
    while [ $E -lt 2000 ];
    do
    RA=$(($A+$B))
    RB=$(($C-$A))
    RC=$(($A*$D))
    RD=$(($A/$E))
    if [ $RA -eq $RB -a $RB -eq $RC -a $RC -eq $RD ];
    then
    echo $A $B $C $D $E
    echo $RA $RB $RC $RD
    echo "That's it !"
    exit 1;
    fi
    E=`expr $E + 1`
    done
   D=`expr $D + 1`
   done
  C=`expr $C + 1`
  done
 B=`expr $B + 1`
 done
A=`expr $A + 1`
done 

Could you help me out with that ?

Regards,
Thomas

RE: Count up several variables ...

Hi

So you intend to run the inner loops multiple times, but you initialize their counters only once, before everything begins ? Not good. Move E=1 immediately before while [ $E -lt 2000 ]; and so on.

But that only fixes the logic. To increase its speed better avoid expr and prefer arithmetic evaluation ( without sigils ! ) :

CODE --> ksh

#!/usr/bin/ksh

A=1
while (( A < 2000 )); do
 B=1
 while (( B < 2000 )); do
  C=1
  while (( C < 2000 )); do
   D=1
   while (( D < 2000 )); do
    E=1
    while (( E < 2000 )); do
    (( RA = A + B ))
    (( RB = C - A ))
    (( RC = A * D ))
    (( RD = A / E ))
    if (( RA == RB && RB == RC && RC == RD )); then
     echo "$A $B $C $D $E"
     echo "$RA $RB $RC $RD"
     echo "That's it !"
     exit 1;
    fi
    (( E++ ))
    done
   (( D++ ))
   done
  (( C++ ))
  done
 (( B++ ))
 done
(( A++ ))
done 

But even so, this will take ages to run. Looks like those Project Euler challenges which can not be solved with brute force using computers currently available for civilians. For example, given that C - A must be equal to A + B which will be always positive, you could safely skip all cases when C < A.

Feherke.
feherke.ga

RE: Count up several variables ...

(OP)
Hi feherke,

thanks a lot, it's working perfect now !

However ... Runtime is a good Point. winky smile
I was really hoping it would be running a "little bit" faster.

I tried to Speed it up a bit but I'm afraid that's still far from what I would Need ... ;-(

CODE

#!/usr/bin/ksh

A=1
while (( A < 2000 )); do
 B=1
 while (( B < 2000 )); do
  C=1
  while (( C < 2000 )); do
   D=1
   while (( D < 2000 )); do
    E=1
    while (( E < 2000 )); do
    (( SUM = A + B + C + D + E ))
    if [ $C -ge $A -a $A -ge $E -a $SUM -le 2000 ];
    then
    (( RA = A + B ))
    (( RB = C - A ))
    (( RC = A * D ))
    (( RD = A / E ))
    if (( RA == RB && RB == RC && RC == RD )); then
     echo "$A $B $C $D $E"
     echo "$RA $RB $RC $RD"
     echo "That's it !"
     exit 1;
    else
    echo "$A $B $C $D $E"
    fi
    else
    if [ $A -lt $E ];
    then
    E=1999
    fi
    fi
    E=`expr $E + 1`
    done
   D=`expr $D + 1`
   done
  C=`expr $C + 1`
  done
 B=`expr $B + 1`
 done
A=`expr $A + 1`
done 

I think I will have to give this some more thought ...

Regards,
Thomas

RE: Count up several variables ...

Hi

Well, to speed it up significantly you will have to move some of the ifs out from the 5th while, so some inner loops can be skipped completely. Is pointless to loop all D's and E's too and only there to check $C -ge $A -- that could be done much earlier in C's block.

By the way, the advice to forget expr is a generic one :

CODE --> Ksh command line

$ time ( A=0; while (( A < 100000 )); do A=`expr $A + 1`; done )             

real    0m32.88s
user    0m3.99s
sys     0m5.34s

$ time ( A=0; while (( A < 100000 )); do (( A++ )); done )

real    0m0.05s
user    0m0.06s
sys     0m0.00s 

Feherke.
feherke.ga

RE: Count up several variables ...

If you define your variables to be Integers, you'll gain a little more speed...

CODE

$ time ( A=0; while (( A < 1000000 )); do (( A++ )); done )           

real    0m0.82s
user    0m0.80s
sys     0m0.00s
$ time ( typeset -i A=0; while (( A < 1000000 )); do (( A++ )); done )

real    0m0.25s
user    0m0.25s
sys     0m0.00s 

So...

CODE

#!/usr/bin/ksh

typeset -i RA
typeset -i RB
typeset -i RC
typeset -i RD

typeset -i A=1
while (( A < 2000 )); do
 typeset -i B=1
 while (( B < 2000 )); do
  typeset -i C=1
  while (( C < 2000 )); do
   typeset -i D=1
   while (( D < 2000 )); do
    typeset -i E=1
    while (( E < 2000 )); do
    (( RA = A + B ))
    (( RB = C - A ))
    (( RC = A * D ))
    (( RD = A / E ))
    if (( RA == RB && RB == RC && RC == RD )); then
     echo "$A $B $C $D $E"
     echo "$RA $RB $RC $RD"
     echo "That's it !"
     exit 1;
    fi
    (( E++ ))
    done
   (( D++ ))
   done
  (( C++ ))
  done
 (( B++ ))
 done
(( A++ ))
done 


RE: Count up several variables ...

As feherke pointed out, this looks like one of those questions that shouldn't be tackled with a brute force attack.
A little bit of common sense and elementary mathematics should suffice, I think.

SPOILER WARNING!
If you insist on finding a solution with the help of a computer program, stop reading here!

As all your numbers are integers >0, we can see:
i) RC = A * D >= A
ii) RD = A / E <= A

RC = RD implies
iii) RC = A and RD = A
and therefore
iv) D = 1
v) E = 1

and finally
A = RC = RA = A + B will give us
vi) B = 0

As the conditions can only be met with B = 0, in contrast to your requirement B > 0, we gather that there is no solution.

Easy, huh?
wink

RE: Count up several variables ...

A star for working smarter, not harder!

bigsmile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close