While looping for until remainder is 0. - 29/09/07 10:08 PM
Trying to write an alias where if I type 2 numbers and the script does a $calc($1 / $2), and if the remainder is 0, stops there, if not, then it keeps dividing and keeps tracks of the remainder. This means if the answer has a decimal, like N.25, it has a remainder, which would be 4. Then at the end, it collects all the remainders, and echo's the order of remainders in reverse..
So doing something like /convert 11 2
11 / 2 = 5.5
0.5 * $2 = 1 (remainder 1)
5 / 2 = 2.5
.5 * $2 = 1 (remainder 2)
2 / 2 = 1 (remainder 3 is 0)
No decimal.
1 / 2 = 0.5 (remainder 4 is 1)
Stops at 0..
So the remainders are 1 1 0 1. Read them backwards, and you get, 1011.
Anyone think they got a shot?
The 1 I made doesn't while loop, it has to stop somewhere, unless the code goes forever...
Thanks.
-Neal.
So doing something like /convert 11 2
11 / 2 = 5.5
0.5 * $2 = 1 (remainder 1)
5 / 2 = 2.5
.5 * $2 = 1 (remainder 2)
2 / 2 = 1 (remainder 3 is 0)
No decimal.
1 / 2 = 0.5 (remainder 4 is 1)
Stops at 0..
So the remainders are 1 1 0 1. Read them backwards, and you get, 1011.
Anyone think they got a shot?
The 1 I made doesn't while loop, it has to stop somewhere, unless the code goes forever...
Thanks.
-Neal.