All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Ternary Multiplication Algorithm (Posted on 2023-03-24) Difficulty: 3 of 5
Provide an algorithm to multiply a series of Base 3 integers, represented as strings, without converting to any other base during any part of the algorithm. Each string (Base 3 integer) may have an arbitrary number of digits.

You may not use arithmetic operations on the digits in a way that is equivalent to changing the base, although you may use a dictionary or table lookup to recognize, for example, that in Base 3, 2*2 = 11.

You can use any programming language, or pseudo-code, or provide a verbal description.

See The Solution Submitted by Larry    
Rating: 5.0000 (1 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Some Thoughts my solution Comment 2 of 2 |
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)]

  Posted by Larry on 2024-04-03 11:45:54
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information