T-Shirt #5

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




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


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

Search DelphiForFun.org only




Problem Description

A fifth entry in our  "Numeric  T-Shirt" line:  

Back of T-Shirt:  "The smallest 3-digit emirp!"

Front:  __ __ __ ?

Background & Techniques

A "emirp" is a prime number which forms a different prime number when its digits are reversed.  (Palindromic primes are excluded by this definition.)  

Not much new work here - we borrowed the IsPrime  and Reverse  functions from T-Shirt  #4 .   So all that is left for the SearchBtnClick method is to run through a loop looking for primes, checking if the reverse is a prime, and checking that they are not the same.  If a number that meets these three conditions, add it to a display list.   

I added a TUpDown box to select how many digits, N,  in the emirps of interest.   A 3-line IntPower function lets us set the lower limit of our search as 10(N-1) and the upper limit at one less than 10 times the lower limit.  For 3-digit emirps for example, that means that at most we'll search from 10(3-1) = 102 = 100 up to 10100-1=1000-1=999.   That pretty much covers the 3-digit candidates.  We search for emirps up to 9 digits, so we'll limit the display to 100 hits - we really don't need thousands (or millions) of them displayed. 

About 50 lines of code here, well within the beginner's range.

Running/Exploring the Program 

bulletBrowse source code
bulletDownload source 
bulletDownload  executable

Suggestions for Further Explorations

This program let's us search for emirps from 2 to 9 digits.  The largest value for 32 bit integer types is a little over 2 billion so numbers up to 999,999,999  can be tested.  Of course int64 type, 64 bit integers,  could test up to somewhere around 1019 if you would prefer the smallest 19-digit emirp on your shirt.    By the way, n binary digits (bits) can represent decimal numbers up to about 0.3n digits.  Why?  Here's a hint, thanks to the miracle of logarithms:  
10? = 2which by taking logarithm of both sides means that 
? log(10) = n log(2) and with a little algebra plus our Windows desktop calculator leads to the conclusion that
? = 0.30103 n  

This is just one of those "reasonableness" checks that can be handy when making quick estimates.       

Why aren't there there any one digit emirps?
What if we wanted the largest n-digit emirp? 


Originally posted: July 27, 2002 Modified:May 15, 2018
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.