 New update is available. Click here to update.

# Power of Three

Last Updated: 15 Mar, 2021
Difficulty: Easy

## PROBLEM STATEMENT

#### Given an integer 'N', you need to determine whether the given integer is a power of 3 or not.

##### Input Format:
``````The first line of input contains an integer ‘T’ representing the number of test cases.

The first and the only line of each test case contains the integer ‘N’ denoting the number.
``````
##### Output Format:
``````For each test case, on a separate line, output one integer 0 or 1. Print 1, if N is a power of 3, or 0 otherwise.

Print the output of each test case in a separate line.
``````
##### Note :
``````You do not need to print anything. It has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 10
1 <= N <= 10^9

Time limit: 1 sec
`````` ## Approach 1

Approach: The idea here is to keep dividing ‘N’ by 3 every time till ‘N’ is divisible by 3. If at the end N is greater than 1 then the number will not be divisible by 3.

The algorithm is as follows :

1. Declare a while loop with condition that ‘N’ % 3 == 0.
• Divide ‘N’ by 3, ‘N’ = ‘N’ / 3.
2. If ‘N’ > 1, return 0 because the given integer is not a power of three.
3. Otherwise, return 1.