Base 3 to integer in Python

Converting a base 3 number to decimal involves multiplying each digit by the appropriate power of 3. Python provides several methods to perform this conversion efficiently.

Understanding Base 3 to Decimal Conversion

In base 3, each digit position represents a power of 3. For example, "10122" in base 3 equals:

1×3? + 0×3³ + 1×3² + 2×3¹ + 2×3? = 81 + 0 + 9 + 6 + 2 = 98

Method 1: Using Horner's Method

This efficient algorithm processes digits from left to right, accumulating the result ?

def base3_to_decimal(s):
    result = 0
    for digit in s:
        result = 3 * result + int(digit)
    return result

# Test the function
base3_number = "10122"
decimal_value = base3_to_decimal(base3_number)
print(f"Base 3 '{base3_number}' = Decimal {decimal_value}")
Base 3 '10122' = Decimal 98

Method 2: Using Built-in int() Function

Python's int() function can directly convert from any base ?

base3_number = "10122"
decimal_value = int(base3_number, 3)
print(f"Base 3 '{base3_number}' = Decimal {decimal_value}")

# Test with different base 3 numbers
test_cases = ["0", "1", "2", "10", "12", "21", "102"]
for base3 in test_cases:
    decimal = int(base3, 3)
    print(f"Base 3 '{base3}' = Decimal {decimal}")
Base 3 '10122' = Decimal 98
Base 3 '0' = Decimal 0
Base 3 '1' = Decimal 1
Base 3 '2' = Decimal 2
Base 3 '10' = Decimal 3
Base 3 '12' = Decimal 5
Base 3 '21' = Decimal 7
Base 3 '102' = Decimal 11

Method 3: Using Mathematical Formula

Calculate using powers of 3 explicitly ?

def base3_to_decimal_formula(s):
    result = 0
    length = len(s)
    
    for i, digit in enumerate(s):
        power = length - i - 1
        result += int(digit) * (3 ** power)
    
    return result

base3_number = "10122"
decimal_value = base3_to_decimal_formula(base3_number)
print(f"Base 3 '{base3_number}' = Decimal {decimal_value}")

# Show the calculation step by step
s = "10122"
print(f"\nStep-by-step calculation for '{s}':")
total = 0
for i, digit in enumerate(s):
    power = len(s) - i - 1
    contribution = int(digit) * (3 ** power)
    total += contribution
    print(f"Position {i}: {digit} × 3^{power} = {digit} × {3**power} = {contribution}")

print(f"Total: {total}")
Base 3 '10122' = Decimal 98

Step-by-step calculation for '10122':
Position 0: 1 × 3^4 = 1 × 81 = 81
Position 1: 0 × 3^3 = 0 × 27 = 0
Position 2: 1 × 3^2 = 1 × 9 = 9
Position 3: 2 × 3^1 = 2 × 3 = 6
Position 4: 2 × 3^0 = 2 × 1 = 2
Total: 98

Comparison

Method Time Complexity Space Complexity Best For
Horner's Method O(n) O(1) Educational, efficient
Built-in int() O(n) O(1) Production code, simplicity
Mathematical Formula O(n) O(1) Understanding the concept

Conclusion

Use Python's built-in int(base3_string, 3) for simplicity and reliability. Horner's method is efficient for custom implementations. The mathematical approach helps understand the underlying conversion process.

Updated on: 2026-03-25T10:13:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements