2 digit Palindrome describes a process by which most multidigit numbers eventually become palindromes. The process being to repeatedly add a given number to the number formed when its digits are reversed.
For example: 152 > 152+251=403 > 403+304=707
I applied that process to a certain three digit number four times before I got a palindrome.
After the first and second additions, it was still a three digit number, and neither was a palindrome.
After the third addition it became a four digit number, but still not a palindrome.
And after the fourth addition the result was a four digit palindrome.
What number did I start with, assuming the first digit was smaller than the last?
The number sought is 192, and the sequence of numbers is:
192
483
867
1635
6996
DEFDBL AZ
FOR n1 = 100 TO 789
d(1) = n1 \ 100
d(2) = (n1 \ 10) MOD 10
d(3) = n1 MOD 10
IF d(1) < d(3) THEN
n2 = n1 + d(3) * 100 + d(2) * 10 + d(1)
IF n2 < 1000 THEN
d(1) = n2 \ 100
d(2) = (n2 \ 10) MOD 10
d(3) = n2 MOD 10
n3 = n2 + d(3) * 100 + d(2) * 10 + d(1)
IF n3 < 1000 AND d(1) <> d(3) THEN
d(1) = n3 \ 100
d(2) = (n3 \ 10) MOD 10
d(3) = n3 MOD 10
n4 = n3 + d(3) * 100 + d(2) * 10 + d(1)
IF n4 >= 1000 AND d(1) <> d(3) THEN
d(1) = n4 \ 1000
d(2) = (n4 \ 100) MOD 10
d(3) = (n4 \ 10) MOD 10
d(4) = n4 MOD 10
n5 = n4 + d(4) * 1000 + d(3) * 100 + d(2) * 10 + d(1)
IF n5 < 10000 AND d(1) <> d(4) THEN
d(1) = n5 \ 1000
d(2) = (n5 \ 100) MOD 10
d(3) = (n5 \ 10) MOD 10
d(4) = n5 MOD 10
IF d(1) = d(4) AND d(2) = d(3) THEN
PRINT n1: PRINT n2: PRINT n3: PRINT n4: PRINT n5: PRINT
END IF
END IF
END IF
END IF
END IF
END IF
NEXT

Posted by Charlie
on 20091127 18:26:08 