1. Computing & Technology

Discuss in my forum

Challenge: Number of Palindromic Numbers

By , About.com Guide

Question: Challenge: Number of Palindromic Numbers
Challenge: provide the implementation of a custom Delphi function to locate all palindromic numbers from 1 to maxNumber and return the number of found palindromes as a result of the function.

The code is submitted for the Number of Palindromic Numbers Delphi Challenge

Answer: Number of palindromic numbers entry by David Reed (USA):
 function DavidReed_NumberOfPalindromes(const maxNumber : int64) : int64;
 var
   x,y,z: integer;
   s: string;
 begin
   s := IntToStr(MaxNumber) ;
   x := Length(s) div 2;
   z := 1;
   if Odd(Length(s)) then begin
     Inc(x) ;
     z := 0;
   end;
   Result := StrToInt(Copy(s,1,x)) + Trunc(Power(10,Length(s) - x)) ;
   for y := 1 to x do begin
     if s[x - y + z] = s[x + y] then
       Continue;
     if s[x - y + z] > s[x + y] then
       Dec(Result) ;
     Break;
   end;
 end;
 

Test data: MAXNUMBER = 10 000 000

David's speed result: 3630 nanoseconds.

Explore the list of all accepted entries for the Number of Palindromic Numbers Delphi Challenge.

Readers Respond: Your Delphi Programming Challenges

©2012 About.com. All rights reserved.

A part of The New York Times Company.