## Count up several variables ...

## Count up several variables ...

(OP)

Hi folks,

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

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

Could you help me out with that ?

Regards,

Thomas

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

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

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

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

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

thanks a lot, it's working perfect now !

However ... Runtime is a good Point.

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

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

Regards,

Thomas

## RE: Count up several variables ...

Well, to speed it up significantly you will have to move some of the ifs out from the 5

^{th}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

Feherke.

feherke.ga

## RE: Count up several variables ...

## CODE

So...

## CODE

## RE: Count up several variables ...

feherkepointed 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?

## RE: Count up several variables ...