A number is n-digit pandigital if it makes use of all the digits
1 to n exactly once.
For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
The sum of the digits 1 through 9 is divisible by 3, so n can't be 9; eliminating the 9 doesn't solve this problem, so n can't be 8. There's no reason n can't be 7, so the program permutes the digits 1 through 7:
DefDbl A-Z
Dim wd(10) As String, w As String, sz, crlf$
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
mx = 0
s$ = "1234567": h$ = s$
Do
n = Val(s)
If prmdiv(n) = n Then
If n > mx Then
mx = n
sv = n
DoEvents
End If
End If
permute s
Loop Until h = s
Text1.Text = Text1.Text & sv & " done" & crlf
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
The answer is:
7652413
|
Posted by Charlie
on 2015-08-07 12:21:30 |