T-Shirt #4

[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.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

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

Search DelphiForFun.org only

 

 

 

Problem Description

The fourth in the "T-Shirt" series:  

Back of T-Shirt:  "Smallest prime integer that remains prime when added to its reversal"

Front:  __ __ __ ?

Background & Techniques

Another beginner's level program that searches for the required number.  If the required prime is a three digit number (it is), then we are searching for the smallest prime number, abc, such that abc+cba is prime. 

There are about 30 user written lines of code here equally divided among three routines:

bulletThe IsPrime function returns true if the integer passed is prime, false if not prime.   
bulletThe Reverse function returns an integer with the digits of the input number reversed.  
bulletAnd the SolveBtnCliick procedure, called when the user clicks the Solve button, loops with a control variable ranging from 13 to 999.    Each value is passed to IsPrime, if result is true the digits are reversed using Reverse function and the sum is passed to IsPrime.   Valid results are displayed in a Listbox.   

Here is the entire source code listing for the three subroutines:

{******* IsPrime *******}
function IsPrime(n:integer):boolean;  {Check is a number is prime}
var i:integer;
begin
    result:=true;
    for i:=2 to trunc(sqrt(n)) do {no need to check higher}
    if i*(i div n)i= n then   {is i a factor of n?}
    begin
        result:=false;
        break;
    end;
end;

{********* Reverse ********}
function Reverse(n:integer):integer;  {reverse the digits of an integer}
begin
    result:=0;
    while n>0 do
    begin
        result:=10*result + n mod 10;
        n:=n div 10;
    end;
end;

{********** SolveBtnClick ********}
procedure TForm1.SolveBtnClick(Sender: TObject);
var i,r:integer;
begin
    for i:= 13 to 999 do
    begin
        if IsPrime(i) then
        begin
            r:=Reverse(i);
            if IsPrime(i+r)
            then listbox1.items.add(inttostr(i) +' + '+inttostr(r)+' = '+inttostr(r+i));
        end; 
    end;
end;

Running/Exploring the Program 

bulletDownload source 
bulletDownload  executable

Suggestions for Further Explorations

Smallest Palindromic prime?  (There's an IsPalindrome function in  T-Shirt #3 program)
Smallest palindromic prime which remains prime when added to its reversal?
Originally posted: May 30, 2002 Modified:May 15, 2018
 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.