- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

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

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

117

True

- Related Questions & Answers
- Check whether sum of digit of a number is Palindrome - JavaScript
- Check whether sum of digits at odd places of a number is divisible by K in Python
- Program to check whether every rotation of a number is prime or not in Python
- Count ways to express a number as sum of powers in C++
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Program to check whether given number is Narcissistic number or not in Python
- 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
- Java Program to Check whether the input number is a Neon Number
- Program to check whether a number is Proth number or not in C++
- Powers of 2 to required sum in C++
- C++ Representation of a Number in Powers of Other
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?

Advertisements