Problem Description

If any two digit number is reversed and added to itself, and the process repeated long enough, a palindromic number will result. A palindromic number is one that reads the same forward and backward.

Which two-digit number requires the most number of operations before a palindromic sum is reached? And how  many steps are required?

As an illustration of the process, take 57: 57+75=132, not palindromic. But the next step, taking 132, reversing it and adding the two gives 132+231=363, which is a palindrome in
two steps!


Source: Math and Logic Problems for PC Enthusiasts, J.J. Clessa, Dover Books, Problem # 45

Background & Techniques

This is a straightforward implementation of the problem description.   For numbers from 2 to 99, we need to reverse each number, add it to the original and then check if the result is a palindrome.

The simplest way to reverse an integer is to convert it to a string, reverse the digits in the string, and then convert the resulting string back to  an integer.   Function Reverse does this.  

To test if a number is a palindrome, we'll convert it to a string, pad it to the to the left with as many zeros as are at the end, and then test if it is a palindrome.  We can do this by comparing digits from each end like this  

result:=true;

For i:= 1 to length(s) do 

    if s[i]<>s[length(s)+1-i] then

    begin

        result:=false;

        break;

   end;

 

Finally, to count the number of steps, we can make a recursive call in the "reverse, sum and test" procedure, passing each sum as the input to the next call until a palindrome is found  or the numbers get too large to check.

 

 

Running/Exploring the Program

Suggestions for further exploration

Try extending the program to 3 digit numbers.