A regional relay-beacon network assigns every beacon a unique positive integer identifier n. Before a beacon is allowed to join the secure mesh, its identifier must pass a cross-base certification check: for every numeral base (radix) b ranging from 2 up to n - 2 inclusive, the digit string obtained by writing n in base b must read the same forwards and backwards (a palindrome). Given a beacon's identifier n, determine whether it passes certification.
A single line containing one integer n.
Print "YES" if the identifier passes certification (its base-b representation is a palindrome for every b from 2 to n - 2 inclusive), otherwise print "NO".
Example 1
Input
4
Expected
NO
Explanation
The only base to check is b = 2 (since the range 2..n-2 collapses to just 2 when n=4). Writing 4 in base 2 gives the digit string "100", which is not a palindrome ("100" reversed is "001"), so certification fails immediately and the answer is NO.
Example 2
Input
6
Expected
NO
Explanation
Checking base b=2 first: 6 in base 2 is "110", which reversed is "011" -- not equal, so certification already fails at the smallest base and the answer is NO. (More generally, every identifier n >= 4 is guaranteed to fail by the time base n-2 is reached, since n written in base n-2 is always the two-digit string "1","2", which can never be a palindrome.)
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →