Add two integers of different base and represent sum in smaller base of the two

Given two integers X, Y in base B1 and B2 respectively, the task is to find the sum of integers X and Y and represent the result in min of ( B1 and B2 ).

Example:

Input: X = 123, Y = 234, B1 = 6, B2 = 8
Output: 543
Explanation:
Integer in base 10: 51 and 156
Sum of Integers: 207
Minimum Base: 6
Sum of Integer in Base 6 = 543

Input: X = 16, Y = 24, B1 = 9, B2 = 7
Output: 45
Explanation:
Minimum Base: 7
Integer in base 7: 21 and 24
Sum of Integer in Base 6 –
   2 1
+   2 4
—————-
   4 5

Approach 1: The idea is to convert both the integer in base 10 ( decimal ) using Base Conversion, then find the sum of the two numbers. The sum is then converted into smaller base B ( minimum of B1 and B2 ) using Base Conversion.



Below is the implementation of the above approach:

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to find the 
# sum of two integers of 
# different bases. 
     
# Base conversion
def dec_to_base(num, base): 
   
    # Maximum base - 36
    base_num = ""
    while num > 0:
        dig = int(num % base)
        if dig < 10:
            base_num += str(dig)
        else:
            # Using uppercase letters
            base_num += chr(ord('A')
                            + dig - 10 )  
        num //= base
    # To reverse the string
    base_num = base_num[::-1
    return int(base_num)
     
# Driver Code  
a = '123'
b = '234'
base_a = 6
base_b = 8
  
# Integer in base 10
a10 = int(a, base_a)
b10 = int(b, base_b)
  
# Sum of integers 
summ = a10 + b10; 
  
# uncomment to check 
# intermediate value 
# of a and b to base 10
# print(a10, b10)
  
# Minimum Base
min_base = min(base_a, base_b)
  
# Sum of integers in Min Base
print(dec_to_base(summ, min_base)) 

chevron_right


Output:

543

Approach 2: We can reduce the number of conversion from previous approach. Here we need to find the smaller base B ( minimum of B1 and B2 ), then convert the integer with larger base to smaller base B and add the integers with help of Addition of two integers of given base.

Below is the implementation of the above approach:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to find the
# sum of two integers of
# different bases.
  
# Base conversion
def dec_to_base(num, base):
    # Maximum base - 36
    base_num = ""
    while num > 0:
        dig = int(num % base)
        if dig < 10:
            base_num += str(dig)
        else:
            # Using uppercase letters
            base_num += chr(ord('A') + dig - 10)
        num //= base
    # To reverse the string
    base_num = base_num[::-1]
    return int(base_num)
  
  
    
# Function to find the sum of
# two integers of base B
def sumBase(a, b, base):
    len_a = len(a)
    len_b = len(b)
  
    s = ""
    sum = ""
  
    diff = abs(len_a - len_b)
  
    # Padding 0 in front of the
    # number to make both numbers equal
    for i in range(1, diff + 1):
        s += "0"
  
    # Condition to check if the strings
    # have lengths mis-match
    if (len_a < len_b):
        a = s + a
    else:
        b = s + b
  
    carry = 0
  
    # Loop to find the find the sum
    # of two integers of base B
    for i in range(max(len_a, len_b) - 1, -1, -1):
  
        # Current Place value for
        # the resultant sum
        curr = carry + (ord(a[i]) - ord('0'))\
               + (ord(b[i]) - ord('0'))
  
        # Update carry
        carry = curr // base
  
        # Find current digit
        curr = curr % base
  
        # Update sum result
        sum = chr(curr + ord('0')) + sum
  
    if (carry > 0):
        sum = chr(carry + ord('0')) + sum
    return sum
  
  
# Driver Code
a = '123'
b = '234'
base_a = 6
base_b = 8
  
# Convert the integer with
# large base to smaller base B
if base_a > base_b:
    min_Base = base_b
    a10 = int(a, base_a)
    a = dec_to_base(a10, base_b)
else:
    min_Base = base_a
    b10 = int(b, base_b)
    b = dec_to_base(b10, base_a)
  
# Sum of Integer in min_Base
sum = sumBase(str(a), str(b), min_Base)
print(sum)

chevron_right


Output:

543

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.