[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
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.
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
For i:= 1 to length(s) do
if s[i]<>s[length(s)+1-i] then
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.
Copyright © 2000-2016, Gary Darby All rights reserved.