How do I check if a number is a palindrome?
Any language. Any algorithm. (except the algorithm of making the number a string and then reversing the string).
How do I check if a number is a palindrome?
Any language. Any algorithm. (except the algorithm of making the number a string and then reversing the string).
Here is an Scheme version that constructs a function that will work against any base. It has a redundancy check: return false quickly if the number is a multiple of the base (ends in 0). And it doesn't rebuild the entire reversed number, only half. That's all we need.
Above most of the answers having a trivial problem is that the int variable possibly might overflow.
Refer to http://leetcode.com/2012/01/palindrome-number.html
I always use this python solution due to its compactness.
For any given num:
If
n == rev
thennum
is a palindrome:Golang version: