Skip to content

326. Power of Three #2047

Answered by mah-shamim
mah-shamim asked this question in Q&A
Aug 13, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to determine if a given integer n is a power of three. An integer n is a power of three if there exists an integer x such that n == 3x.

Approach

The approach leverages mathematical properties of powers of three within the constraints of 32-bit signed integers. The key insight is that the largest power of three that fits within a 32-bit signed integer is 319 = 1162261467. For any positive integer n to be a power of three, it must be a divisor of 319. This is because the divisors of 319 are exclusively 3k for k = 0 to 19, which covers all possible powers of three within the 32-bit signed integer range.

  1. Check for Non-Positive Values: If n is less than or equal to zero, it cannot be …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Aug 13, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Aug 13, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants