I'll present my solution (in Python) here.
To use the function multiply_Base_3(), the arguments are a series of one or more base 3 numbers (as integers or text). The function returns a list of the product in base 3 and converted to decimal.
The algorithm makes use of lookup tables as Python dictionaries.
def base2base(n,a,b): # just for checking results
""" input n which is a string of the number to be changed
'a' is an integer representing the current base
to be changed to base b
"""
def dec2base(i,base):
""" INPUT integer in base 10, return string
of Base base equivalent. """
convertString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
if i < base:
return convertString[i]
else:
return dec2base(i//base,base) + convertString[i%base]
if a == 10:
return dec2base(int(n),b)
if b == 10:
return int(str(n),a)
elif b != 10:
return base2base(int(str(n),a),10,b)
sum3 = {('0','0'):'0', ('0','1'):'1', ('0','2'):'2', ('1','0'):'1', ('1','1'):'2', ('1','2'):'10', ('2','0'):'2', ('2','1'):'10', ('2','2'):'11'}
prod3 = {('0','0'):'0', ('0','1'):'0', ('0','2'):'0', ('1','0'):'0', ('1','1'):'1', ('1','2'):'2', ('2','0'):'0', ('2','1'):'2', ('2','2'):'11'}
def addition_Base_3(a,*args):
""" arguments are a series of multiple digit base 3 integers as strings.
Return their sum in base 3 """
ans = a
for arg in args:
a = str(ans)
b = str(arg)
if len(a) > len(b):
b = '0'*(len(a) - len(b)) + b
if len(b) > len(a):
a = '0'*(len(b) - len(a)) + a
thesum = ''
carry = '0'
for i in reversed(range(len(a))):
temp = sum3[a[i], b[i]]
if len(temp) == 1:
temp = sum3[temp, carry]
else:
temp = temp[0] + sum3[temp[1], carry]
if len(temp) == 1:
carry = '0'
thesum = temp[0] + thesum
else:
carry = '1'
thesum = temp[1] + thesum
ans = (carry + thesum).lstrip('0')
return ans
def multiply_Base_3(a,*args):
""" arguments are a series of multiple digit base 3 integers as strings.
Return their product in base 3 """
ans = a
for arg in args:
a = str(ans)
b = str(arg)
ans = 0
for j,d in enumerate(reversed(b)):
for i,c in enumerate(reversed(a)):
ans = addition_Base_3(ans , prod3[(c,d)] + '0'*(i+j) )
return [ans, base2base(ans, 3,10)]