2 digit Palindrome describes a process by which most multi-digit 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?
I originally set this up in a spreadsheet and it quickly became obvious that the hundreds digit had to be "1" and that the units was "2". It was then just a matter of deciding upon the tens value.
During review a note was made about there being more that one possibility.
Unsure whether that reviewer had not immediately realised that there was a stipulation that the first digit was lower than the last. However I wrote the following program allows for zero to be the first digit.
We would
not normally consider a three-digit integer beginning with a leading zero to be three-digit. That said the program finds
093 and 192 as solutions, both yielding 483 on the first addition.
It is howevr clear that the required three-digit is
192.
CLS
FOR a = 0 TO 8
FOR b = 0 TO 9
FOR c = a + 1 TO 9
n1 = a * 100 + b * 10 + c
n2 = c * 100 + b * 10 + a
n3 = n1 + n2
IF n3 < 1000 THEN
d = INT(n3 / 100)
e = INT((n3 - d * 100) / 10)
f = n3 - d * 100 - e * 10
IF d <> f THEN
n4 = f * 100 + e * 10 + d
n5 = n3 + n4
IF n5 < 1000 THEN
g = INT(n5 / 100)
h = INT((n5 - g * 100) / 10)
i = n5 - g * 100 - h * 10
IF g <> i THEN
n6 = i * 100 + h * 10 + g
n7 = n5 + n6
IF n7 > 1000 THEN
j = INT(n7 / 1000)
k = INT((n7 - j * 1000) / 100)
l = INT((n7 - j * 1000 - k * 100) / 10)
m = n7 - j * 1000 - k * 100 - l * 10
IF j <> m AND k <> l THEN
n8 = m * 1000 + l * 100 + k * 10 + j
n9 = n7 + n8
IF n9 < 10000 THEN
n = INT(n9 / 1000)
o = INT((n9 - n * 1000) / 100)
p = INT((n9 - n * 1000 - o * 100) / 10)
q = n9 - n * 1000 - o * 100 - p * 10
IF n = q AND o = p THEN
PRINT a; b; c
PRINT n1; n2, n3; n4, n5; n6, n7; n8, n9
END IF
END IF
END IF
END IF
END IF
END IF
END IF
END IF
NEXT
NEXT
NEXT
|
Posted by brianjn
on 2009-11-27 22:17:05 |