An   emirp   (prime spelled backwards)   are primes that when reversed   (in their decimal representation)   are a different prime.

(This rules out palindromic primes.)



Task

   show the first   twenty   emirps
   show all emirps between   7,700   and   8,000
   show the   10,000th   emirp


In each list, the numbers should be in order.

Invoke the (same) program once per task requirement, this will show what limit is used as the upper bound for calculating surplus (regular) primes.

The specific method of how to determine if a range or if specific values are to be shown will be left to the programmer.



See also

   Wikipedia, Emirp.
   The Prime Pages, emirp.
   Wolfram MathWorld™, Emirp.
   The On‑Line Encyclopedia of Integer Sequences, emirps (A6567).


' FB 1.05.0 Win64
 
Function isPrime(n As UInteger) As Boolean
  If n < 2 Then Return False
  If n Mod 2 = 0 Then Return n = 2
  If n Mod 3 = 0 Then Return n = 3
  Dim d As Integer = 5
  While d * d <= n
    If n Mod d = 0 Then Return False
    d += 2
    If n Mod d = 0 Then Return False
    d += 4
  Wend
  Return True
End Function
 
Function reverseNumber(n As UInteger) As UInteger
  If n < 10 Then Return n
  Dim As Integer sum = 0
  While n > 0
    sum = 10 * sum  + (n Mod 10)
    n \= 10
  Wend
  Return sum
End Function
 
Function isEmirp(n As UInteger) As Boolean
  If Not isPrime(n) Then Return False
  Dim As UInteger reversed = reverseNumber(n)
  Return reversed <> n AndAlso CInt(isPrime(reversed))
End Function
 
' We can immediately rule out all primes from 2 to 11 as these are palindromic
' and not therefore Emirp primes
Print "The first 20 Emirp primes are :"
Dim As UInteger count = 0, i = 13
Do
  If isEmirp(i) Then
    Print Using "####"; i;
    count + = 1
  End If
  i += 2
Loop Until count = 20
Print : Print
Print "The Emirp primes between 7700 and 8000 are:"
i = 7701
Do
  If isEmirp(i) Then Print Using "#####"; i;
  i += 2
Loop While i < 8000
Print : Print
Print "The 10,000th Emirp prime is : ";
i = 13 : count = 0
Do
  If isEmirp(i) Then count += 1
  If count = 10000 Then Exit Do
  i += 2
Loop
Print i
Print
Print "Press any key to quit"
Sleep