Problem 543
Power of Three
Easy · 10 pts · Math
Problem
Decide whether an integer N is a power of three, that is, whether N = 3^k for some integer k >= 0. Note that 1 = 3^0 is a power of three, while zero and negative numbers are not. Keep dividing N by 3 while it is divisible; it is a power of three exactly when you end at 1. Alternatively, a positive N is a power of three when it divides 3^19 = 1162261467, the largest power of three that fits in 32 bits.
Input
A single integer N.
Output
Print YES if N is a power of three, otherwise NO.
Constraints
-2^31 <= N <= 2^31 - 1
Example input
27
Example output
YES
Explanation
27 = 3 × 3 × 3 = 3^3, so it is a power of three and the answer is YES. 45 = 3^2 × 5 would be NO because of the factor 5.