62 lines
1.3 KiB
Text
62 lines
1.3 KiB
Text
' 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
|