Pattern visualizer
Palindrome Number
The key insight: a number is a palindrome if and only if reversing its digits mathematically produces the same value. We can reverse a number without string conversion by repeatedly extracting the last digit with x % 10 and discarding it with Math.floor(x / 10), building the reversed number digit by digit. Once reversed, we compare it to the original — if they match, the number reads the same forward and backward. Animated on: Given an integer x, determine if it's a palindrome by reversing its digits using mod and divide (no string conversion)..
Math
original = x = 121. We'll reverse x digit by digit, storing the result in reversed.
1FUNCTION isPalindrome(x):2 IF x < 0: RETURN false3 original = x4 reversed = 05 WHILE x > 0:6 digit = last digit of x (x mod 10)7 reversed = reversed * 10 + digit8 drop the last digit of x (integer divide x by 10)9 RETURN true if reversed equals original, otherwise false
← / → step · space play · Home restart