There are two arithmetic increasing sequences of 3 terms each (4-digit prime numbers), such that each of their terms is a permutation of another.
Find both.
1487 4817 8147
2969 6299 9629
from
DefDbl A-Z
Dim crlf$
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
p1 = 999
Do
DoEvents
p1 = nxtprm(p1)
If p1 <= 9995 Then
p2 = p1
r = 9999 - p1
Do
p2 = nxtprm(p2)
If p2 <= p1 + r / 2 Then
diff = p2 - p1
p3 = p2 + diff
If prmdiv(p3) = p3 And p3 <= 9999 Then
If isPerm(Str(p1), Str(p2)) And isPerm(Str(p2), Str(p3)) Then
Text1.Text = Text1.Text & p1 & Str(p2) & Str(p3) & crlf
End If
End If
End If
Loop Until p2 > 9997
End If
Loop Until p1 > 9999
End Sub
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
n = n + 1
Wend
nxtprm = n
End Function
Function isPerm(a$, b$)
x$ = a$
good = 1
If Len(a$) <> Len(b$) Then isPerm = 0: Exit Function
For i = 1 To Len(b$)
ix = InStr(x, Mid(b, i, 1))
If ix = 0 Then good = 0: Exit For
x = Left(x, ix - 1) + Mid(x, ix + 1)
Next
isPerm = good
End Function
|
Posted by Charlie
on 2015-12-18 11:00:18 |