Palindromic Sums

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

 

Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

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.  

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2016, Gary Darby    All rights reserved.