Devise an algorithm to determine the direct conversion of a positive integer from base A to base B, without going through base 10.
If B=10 it is of course easy, but why? Because we think and do arithmetic in base 10. So I think our algorithm needs the ability to "think" and do arithmetic in any base.
Also I believe the input in base A, call it "a", probably has to be read in as a string. Not doing so would disallow bases greater than 10, plus I think just storing it as a number would be storing the base 10 version of the same sequence of digits.
But the bases A and B presumably need to be in base 10 or we would not know what bases are intended. Any number in its own base just looks like "10".
Construct functions that do basic integer math operations in any base, such as:
Add(n,m,base) which takes strings "n" and "m" and manipulates them according to the rules of addition in base "base" and then outputs a string representation of the result in that same base.
Similarly for Subtract, Multiply, Divide, Power etc.
Also: Increment(n,base) and Decrement(n,base).
We probably need a string array where the elements are the string version of the various digits: "0,1,2,3,4,5,6,7,8,9,A,B,C,D, ..." etc.
I would prefer an algorithm that takes the sequence of digits, "a" and the bases A and B, marches through one digit at a time and converts to the new base on the fly, but I have not been able to solve that.
But the following pseudo code should get the answer, assuming the functions work
Input: a ; as string
Input: A, B ; as base 10 integers
Start_value_a = a
b = 0
Print "Initial value " Start_value_a " in base " A " has been converted to " b " in base " B
Posted by Larry
on 2014-07-07 11:33:46