What is the highest prime number with no repeated digits?
The largest prime with no repeated digits is
987654103
It couldn't have 10 digits, as that would be a number divisible by 9, so a digit not divisible by 3 had to be removed to make it indivisible by 3. To give it the maximal number of remaining digits it needed a zero to give it nine full digits, but not at either the end nor the beginning.
The answer does indeed lack the digit 2, and has its zero in the second to last position. All the candidates were tested by
DefDbl A-Z
Dim crlf$
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
s$ = "123456789": h$ = s
Do
If Val(Right(s, 1)) Mod 3 <> 0 Then
s1$ = Left(s, 8)
For p = 1 To 7
s2$ = Left(s1, p) + "0" + Mid(s1, p + 1)
v = Val(s2)
pd = prmdiv(v)
If pd = v Then
If v > mx Then mx = v
End If
Next
End If
DoEvents
permute s
Loop Until s = h
Text1.Text = mx
Form1.Visible = True
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
|
Posted by Charlie
on 2018-12-12 15:15:17 |