Number Conversion help
Author |
Message |
Klick
|
Posted: Thu May 18, 2006 5:57 pm Post subject: Number Conversion help |
|
|
As a part of my ecryption program for class, I would 5 numbers to be put into 1 large number, by the following sequence:
8,24,7,0,3
Large Number = 8 * (27**4) + 24 * (27**3) + 7 * (27**2) + 0 * (27**1) + 3 * (27 **0)
The number in this case would be 4729026.
Converting the 5 numbers into 1 number was fairly simple to figure out on my own. I need help recovering numbers from that large number.
I figure that i can do this using mod by the series of powers of 27, and then subtract the remainder from the total
Like so:
code: | var largenum : int
var smallnums : array 1 .. 5 of int
var power :int := 1
largenum := 8 * (27 ** 4) + 24 * (27 ** 3) + 7 * (27 ** 2) + 0 * (27 ** 1) + 3 * (27 ** 0)
for decreasing i : 5 .. 1
smallnums (i) := largenum
smallnums (i) := smallnums (i) mod 27 ** power
largenum -= smallnums (i)
power += 1
end for
for i : 1 .. 5
put smallnums (i), "," ..
end for
|
It works for the last 2 numbers for some, reason, but fails miserably on the first 3.
The problem I think, is the way I am approching the solution, or I am missing something important. Any suggestions or help will be greatly appreciated.
Much Thanks! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: Thu May 18, 2006 6:27 pm Post subject: (No subject) |
|
|
code: |
for decreasing i : 4 .. 0
put largenum div 27 ** i
largenum -= (largenum div 27 ** i) * 27 ** i
end for
|
Yarr, sorry for doing it entirely for you, its generally against my policy to do that, but I couldn't get this one after half a second of looking at it, so I tinkered with it till I got it
I will now attempt to explain where you went wrong.
code: |
for decreasing i : 5 .. 1
smallnums (i) := largenum
smallnums (i) := smallnums (i) mod 27 ** power
largenum -= smallnums (i)
power += 1
end for
|
The above is your code. Lets pick an arbitrary letter to represent what the first number is. I like n, so lets go with that. If we divide the "largenum" by 27**4, the resulting number will be between n * 27**4 and (n+1)*27**4. Therefore, if we truncate that number, we will get what n is. Now, just subtract n * 27**4 from the large number and repeat. One other error you made in yours is "power" started at 1, when it should have started at zero (look at the largenum calculation). |
|
|
|
|
![](images/spacer.gif) |
Klick
|
Posted: Thu May 18, 2006 6:37 pm Post subject: (No subject) |
|
|
You really are TheOneTrueGod!! I knew i was missing something in it, just couldn't figure it out. Thanks a bunch. This will really help me out alot.
Thanks again! |
|
|
|
|
![](images/spacer.gif) |
|
|