Project 98: Base K to Base M

Base K to Base N (converting from and to any base)

Since we can convert decimal to and from any base, we can extend this to convert any base to any other base.

Example 1: Convert 56 base 7 to base 4.

56 base 7 to decimal: 5*7^1 + 6*7^0 = 41 base 10

41 base 10 to base 4: 2*4^2 + 2*4^1 + 1*2^0.

Therefore, 56 base 7 is equivalent to 221 base 4.

Example 2: Convert 2221 base 3 to base 5.

2221 base 3 to decimal: 2*3^3 + 2*3^2 + 2*3^1 + 1*3^0 = 54 + 18 + 6 + 1 = 79 base 10

79 base 10 to base 5: 3*5^2 + 0*5^1 + 4*5^0.

Therefore, 2221 base 3 is equivalent to 304 base 5.


Project 98: Variables 'fromNumber', 'fromBase', 'toBase' have been initialized.  There are also working methods decimalToBaseK and baseKToDecimal.

fromNumber is the number you are converting as an array: [5, 6] in the example above.

fromBase is the base of the original number: 7 in the example above.

toBase is the base you are converting to: 4 in the example above.

decimalToBaseK(decimal, base) returns an array of digits that is equivalent to decimal in the given base.

Example: decimalToBaseK(41, 4) returns [2, 2, 1].

baseKToDecimal(base, baseKNumber) returns the decimal equivalent of the baseKNumber.

Example: baseKToDecimal(7, [5,6]) returns 41.

Task: Appropriately assign the value of the variable 'toNumber' to be equivalent number with the given base (toNumber is an array of digits)

Example: if fromNumber = [5, 6], fromBase = 7, toBase = 4, then toNumber should be [2, 2, 1].

**If your code works for 5 test cases, you can enter your e-mail address

Universal Computational Math Methods:

pow(5,2) returns 25.0

abs(-3.0) returns 3

sqrt(49.0) returns 7.0