Program to check whether number is a sum of powers of three in Python

Suppose we have a number n, we have to check whether it is possible to represent n as the sum of distinct powers of three or not. An integer y is said to be power of three if there exists an integer x such that y = 3^x.

So, if the input is like n = 117, then the output will be True because 117 = 3^4 + 3^3 + 3^2 = 81 + 27 + 9.

Algorithm

To solve this, we will follow these steps ?

  • for i in range 16 to 0, decrease by 1, do

    • if n >= 3^i, then

      • n := n − 3^i

  • if n > 0, then

    • return False

  • return True

Example

Let us see the following implementation to get better understanding ?

def solve(n):
    for i in range(16, -1, -1):
        if n >= pow(3, i):
            n -= pow(3, i)
    
    if n > 0:
        return False
    
    return True

n = 117
print(solve(n))

The output of the above code is ?

True

How It Works

The algorithm works by using a greedy approach. Starting from the largest possible power of three (3^16), we check if it can be subtracted from n. If yes, we subtract it and continue with the next smaller power. This ensures we use distinct powers of three.

def solve(n):
    print(f"Checking if {n} can be represented as sum of distinct powers of 3")
    original_n = n
    
    for i in range(16, -1, -1):
        power_of_three = pow(3, i)
        if n >= power_of_three:
            print(f"Using 3^{i} = {power_of_three}")
            n -= power_of_three
    
    print(f"Remaining value: {n}")
    return n == 0

# Test with different numbers
test_numbers = [117, 10, 27, 40]
for num in test_numbers:
    result = solve(num)
    print(f"{num}: {result}")
    print("---")
Checking if 117 can be represented as sum of distinct powers of 3
Using 3^4 = 81
Using 3^3 = 27
Using 3^2 = 9
Remaining value: 0
117: True
---
Checking if 10 can be represented as sum of distinct powers of 3
Using 3^2 = 9
Using 3^0 = 1
Remaining value: 0
10: True
---
Checking if 27 can be represented as sum of distinct powers of 3
Using 3^3 = 27
Remaining value: 0
27: True
---
Checking if 40 can be represented as sum of distinct powers of 3
Using 3^3 = 27
Using 3^2 = 9
Using 3^1 = 3
Using 3^0 = 1
Remaining value: 0
40: True
---

Alternative Approach Using Base-3 Representation

We can also solve this problem by converting the number to base-3 and checking if all digits are 0 or 1 ?

def solve_base3(n):
    while n > 0:
        if n % 3 == 2:
            return False
        n //= 3
    return True

# Test both approaches
numbers = [117, 10, 26, 40]
print("Number | Greedy | Base-3")
print("-------|--------|-------")
for num in numbers:
    greedy_result = solve(num)
    base3_result = solve_base3(num)
    print(f"{num:6} | {greedy_result:6} | {base3_result}")
Number | Greedy | Base-3
-------|--------|-------
   117 |   True | True
    10 |   True | True
    26 |  False | False
    40 |   True | True

Conclusion

The greedy approach works by subtracting the largest possible powers of three from the number. If we can reduce the number to exactly zero, it means the original number can be represented as a sum of distinct powers of three. The base-3 approach is more efficient as it only requires checking if any digit in the base-3 representation is 2.

Updated on: 2026-03-26T14:14:46+05:30

640 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements