# # "Re-basing" a number is the process of taking a number in one base (in this # case, base 10) and expressing the same value in another numeric base. # In all cases, numerical representations express *power* and *terms*. # Whenever you have a number, <1><2><3><4>bX, the actual value of that # number is: # <4> * X ** 0 # + <3> * X ** 1 # + <2> * X ** 2 # + <1> * X ** 3 # # Thus, the process of converting one number into another base is simply # a matter of calculating the values of <1> <2> <3> <4> and so on, until # the number is exhausted. # # There are two ways to calculate this value. You can go forwards, or # backwards. The way you go doesn't matter much, but it may to your teacher. # Since the new representation of the number is a determinite value, it # does not matter how you arrive at the result. Let's convert 500 into # base 3 on paper: # # To begin, it helps to calculate how many digits will be in the new number. # There exists exactly one number X such that: # # base ** X <= num and base ** (X + 1) > num # # (X + 1) is the number of digits in the new number. In this case... # 3 ** 1 -> 3 3 ** 2 -> 9 3 ** 3 -> 81 # 3 ** 4 -> 243 3 ** 5 -> 729 # # Well, 243 < 500 < 729, so X is 4 -- and there will be five digits in the # answer. Now we just need to find what the five values are. # # ### Going forward ### # # There exists one set of numbers [a, b, c, d, e] such that: # (a * 3 ** 4) + (b * 3 ** 3) + (c * 3 ** 2) + (d * 3 ** 3) + e == 500 # such that [a, b, c, d] are integers. # # a * 243 # + b * 81 # + c * 9 # + d * 3 # + e # ========= # 500 # # So what is the value of 'a'? Well, it is simply the floor of # 500 / 243 -- 2. Now we know what 'a' is, we can remove it from the # equation: # # 2 * 243 (486) # + b * 81 # + c * 9 # + d * 3 # + e # ========= # 500 # Subtracting 486 from both sides, we're left with: # # b * 81 # + c * 9 # + d * 3 # + e # ======== # 14 # # What is the value of 'b'? Why it floor(14 / 81) -- 0. # # c * 9 # + d * 3 # + e # ======= # 14 # # What is the value of 'c'? It is floor(14 / 9) -- 1. That leaves us with: # # d * 3 + e == 5 # # So we know that d is 1. That leaves us with # # e = 2 # # So now we have our result: # # [2, 0, 1, 1, 2] # # Thus, 500b10 == 20112b3 # # Viola! # # ### Going backward ### # # There exists one set of numbers [a, b, c, d, e] such that: # (a * 3 ** 4) + (b * 3 ** 3) + (c * 3 ** 2) + (d * 3 ** 3) + e == 500 # such that [a, b, c, d] are integers. # # a * 243 # + b * 81 # + c * 9 # + d * 3 # + e (* 1) # ========= # 500 # # So what is the value of 'e'? This is more complicated -- it is the # number of times that this multiplier (1) goes into the remainder (modulus) # of the previous multiplier (3), in this case, (500 % 3) / 1 == 2 # # Now that we know what 'e' is, we can remove it from the equation: # # a * 243 # + b * 81 # + c * 9 # + d * 3 # + 2 # ========= # 500 # Subtracting 2 from both sides, we're left with: # # a * 243 # + b * 81 # + c * 9 # + d * 3 # ========= # 498 # What is the value of 'd'? It is (498 % 9) / 3 == 1 # # a * 243 # + b * 81 # + c * 9 # ========= # 495 # Calculating for 'c', (495 % 81) / 9 == 1 # # a * 243 # + b * 81 # ========= # 486 # Calcuating for 'b', (486 % 243) / 81 == 0 # # So a * 243 == 486, and it doesn't take a rocket scientist to see that a == 2. # # So our result is [2, 0, 1, 1, 2], therefore 500b10 == 20112b3. #