An
emirp (prime spelled backwards) is a prime number that results in a different prime when its decimal digits are reversed.
This definition excludes the related palindromic primes.
13, 17, 31, 37, 71... are
emirps.
List values of q(n) (the number of emirps of length n) for n=2 to 6.
The census is shown:
emirps pal emirp
pairs
1 0 4 0
2 8 1 4
3 28 15 14
4 204 0 102
5 1406 93 703
6 9538 0 4769
7 70474 668 35237
The first column after the length column shows the number of primes that fit. The next two columns show the number that are palindromes, and then the number of pairs of non-palindromic primes (half the value in column 1). Lengths 4 and 6 have no palindromic primes as palindromes with those numbers of digits would be multiples of 11.
DefDbl A-Z
Dim crlf$, ct(7), pal(7)
Function mform$(x, t$)
A$ = Format$(x, t$)
If Len(A$) < Len(t$) Then A$ = Space$(Len(t$) - Len(A$)) & A$
mform$ = A$
End Function
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
p = 1
Do
p = nxtprm(p)
forw$ = LTrim(Str(p))
rev$ = ""
For i = 1 To Len(forw$)
rev = Mid(forw, i, 1) + rev
Next
If Len(rev) > 7 Then Exit Do
If prmdiv(Val(rev)) = Val(rev) Then
l = Len(rev)
ct(l) = ct(l) + 1
If Val(rev) = p Then pal(l) = pal(l) + 1
If l = 2 Then Text1.Text = Text1.Text & Str(p)
End If
DoEvents
Loop
Text1.Text = Text1.Text & crlf
For l = 1 To 7
Text1.Text = Text1.Text & l & mform(ct(l) - pal(l), "#####0") & mform(pal(l), "####0")
Text1.Text = Text1.Text & mform((ct(l) - pal(l)) / 2, "#####0") & crlf
Next
End Sub
Function prm(i)
Dim p As Long
Open "17-bit primes.bin" For Random As #111 Len = 4
Get #111, i, p
prm = p
Close 111
End Function
Function prmdiv(num)
Dim n, dv, q
If num = 1 Then prmdiv = 1: Exit Function
n = Abs(num): If n > 0 Then limit = Sqr(n) Else limit = 0
If limit <> Int(limit) Then limit = Int(limit + 1)
dv = 2: GoSub DivideIt
dv = 3: GoSub DivideIt
dv = 5: GoSub DivideIt
dv = 7
Do Until dv > limit
GoSub DivideIt: dv = dv + 4 '11
GoSub DivideIt: dv = dv + 2 '13
GoSub DivideIt: dv = dv + 4 '17
GoSub DivideIt: dv = dv + 2 '19
GoSub DivideIt: dv = dv + 4 '23
GoSub DivideIt: dv = dv + 6 '29
GoSub DivideIt: dv = dv + 2 '31
GoSub DivideIt: dv = dv + 6 '37
Loop
If n > 1 Then prmdiv = n
Exit Function
DivideIt:
Do
q = Int(n / dv)
If q * dv = n And n > 0 Then
prmdiv = dv: Exit Function
Else
Exit Do
End If
Loop
Return
End Function
Function nxtprm(x)
Dim n
n = x + 1
While prmdiv(n) < n Or n < 2
n = n + 1
Wend
nxtprm = n
End Function
|
Posted by Charlie
on 2018-10-12 12:05:22 |