Power of Three

Updated on 03 July, 2025
Power of Three header image

Problem Statement

The task is to determine whether a given integer n can be expressed as 3^x for some integer x. An integer that satisfies this condition is said to be a "power of three." The function should return true if n is a power of three and false otherwise. Specifically, for an integer to be a power of three, there must exist an integer x such that the relationship n == 3^x holds true. This statement remains valid under the condition that n falls within the range of a 32-bit signed integer.

Examples

Example 1

Input:

n = 27

Output:

true

Explanation:

27 = 33

Example 2

Input:

n = 0

Output:

false

Explanation:

There is no x where 3x = 0.

Example 3

Input:

n = -1

Output:

false

Explanation:

There is no x where 3x = (-1).

Constraints

  • -231 <= n <= 231 - 1

Approach and Intuition

From the examples given and the constraints, several key points can be drawn on how the check for a power of three is performed and the general behavior of powers of three:

  1. Basic Understanding:

    • A power of three is fundamentally governed by exponential growth specific to base three. So every valid power of three is some integer exponentiation of three.
  2. Integer Constraints:

    • Powers of three will grow much faster than linear sequences. This is essential when considering large positive or negative bounds of n.
    • Although direct multiplication might seem an initially intuitive way to check each power of three, the exponential growth means that very few checks are actually needed to reach the upper bound of 32-bit signed integers.
  3. Handling Zero and Negatives:

    • Zero and any negative number are directly not powers of three because no exponent of a positive base (like 3) could result in zero or a negative number.
  4. Iterative Validation:

    • One practical approach is to use a loop that begins from 3^0 = 1 and successively multiplies by 3, checking if it equals n, till the product remains within the int range. When n matches the power of three during these iterations, return true.
    • If none match, returning false after the loop ends is the logical conclusion.
  5. Optimizations:

    • Knowing that starting from 3^0 and moving upwards can be stopped once the current power of three surpasses n provides a neat boundary to the loop, increasing efficiency.

By following these points and moving step-wise through possible powers of three, we can decide whether n is a power of three without checking extraneous cases or performing needless calculations. This direct approach, leveraging some properties of exponential growth and bounds checking, provides an efficient and clear method for solving the problem within the given constraints.

Solutions

  • Java
java
public static void main(String[] args) {
    Solution instance = new Solution();
    int count = 1; // Repetition count
    for (int index = 0; index < count; index++) {
        instance.isPowerOfThree(index);
    }
}

The provided Java code snippet aims to determine whether a number is a power of three. It involves creating an instance of a Solution class and utilizing its isPowerOfThree method within a for loop to check if the number represented by the index variable is a power of three. The for loop runs a number of times equal to the count variable, set initially to 1. This setup means the method will check this condition only once, for the number 0. If additional logic or implementations are part of the isPowerOfThree method, they are not shown here, and essential further details would need to be added for a complete functional implementation of this task.

Comments

No comments yet.