1. Home
  2. Computing & Technology
  3. Delphi Programming

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
Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Source Code Projects
  5. Challenges / Exercises
  6. Number of Palindromic Numbers Delphi Challenge Entry by David Reed

©2010 About.com, a part of The New York Times Company.

All rights reserved.