- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

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))

## Input

117

## Output

True

- Related Articles
- Check whether sum of digit of a number is Palindrome - JavaScript
- Program to check whether every rotation of a number is prime or not in Python
- Check whether sum of digits at odd places of a number is divisible by K in Python
- Program to check whether given number is Narcissistic number or not in Python
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Count ways to express a number as sum of powers in C++
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Program to check whether list can be partitioned into pairs where sum is multiple of k in python
- Program to check whether a number is Proth number or not in C++
- Swift Program to Check whether the input number is a Neon Number
- Java Program to Check whether the input number is a Neon Number
- Haskell Program to Check whether the input number is a Neon Number
- Program to check whether each node value except leaves is sum of its children value or not in Python
- C++ Program to Check Whether a Number is Prime or Not

Advertisements