Power of Three
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Description
Given an integer num, write a method to determine if it is a power of 3.

The method will be called as follows:
console.log(powerOfThree(9));
// true
console.log(powerOfThree(7));
// false
Constraints
- The given would be a non zero positive integer in the range between
1and2147483647 - Expected space complexity :
O(logn) - Expected time complexity :
O(1)
```js
function powerOfThree(num) {
// Fill in this method
return num;
}
```
Multiple Choice
In the expression 3^6 what does the ^6 stand for?
Multiple Choice
What is a loop in programming terminology?
Fill In
In programming, the while-loop can be considered as a repeating _____ statement.
Multiple Choice
Identify the correct exponential expression for the integer 729.
True or False?
In programming, the exponential value of a number cannot be determined without loops.
Completion
The easiest way to go about this is to think about how exponents work. When you see 3^6, you are multiplying 3 by 3 exactly 6 times.
With that said, we can simply reverse the direction: we can take the result, and continuously divide it by 3. If we eventually get to 1, we know that it's been divisible all along and is thus an exponent of 3.

Complexity of Final Solution
Since we are dividing by 3 for every iteration in the while loop, we find our answer after roughly log3(n) (base 3) iterations for O(logn) time complexity. We do not use any additional data structures, so we use O(1) constant space.
One Pager Cheat Sheet
One Pager Cheat Sheet
- Write a method
powerOfThreewhich determines if a given non zero positive integernumbetween1and2147483647is apower of 3, with an expected space complexity ofO(logn)and an expected time complexity ofO(1). - The
^6in3^6stands for the exponent calculated using the formulabase^exponent = result. - Loops are a useful control structure in programming used to repeat certain code
multiple times, saving the developer time when dealing with large data sets or unknown iterations. - The
while-loopcan be considered as a repeatingIF-statementbecause it needs an initial evaluation of the condition before continuing to repeat a certain set of instructions as long as the condition is true. - The number 729 can be expressed using
exponential notationas3^6, since it is the result of raising 3 to the 6th power. - The exponential value of any number can be calculated
without loops, simply by raising that number to the power of itself. - We can solve this problem in
O(logn)time complexity andO(1)space complexity by continuously dividing the number by3.