Function Miller(ByVal p As Long, ByVal iteration As Integer) As Boolean
If p < 2 Then
Return False
Exit Function
ElseIf p <> 2 And p Mod 2 = 0 Then
Return False
Exit Function
Else
Dim s As Long = p - 1
While s Mod 2 = 0
s = s / 2
End While
For i = 1 To iteration
Dim a As Long = Random(1, p - 1)
Dim temp As Long = s
Dim Modu As Long = (a Mod temp) Mod p
While temp <> p - 1 And Modu <> 1 And Modu <> p - 1
Modu = mulmod(Modu, Modu, p)
temp = 2 * temp
End While
If Modu <> p - 1 And temp Mod 2 = 0 Then
Return False
Exit Function
End If
Next
Return True
End If
End Function
Function mulmod(ByVal a As Long, ByVal b As Long, ByVal c As Long) As Long
Dim x, y As Long
x = 0
y = a Mod c
While b > 0
If b Mod 2 = 1 Then
x = (x + y) Mod c
End If
y = (y * 2) Mod c
b = b / 2
End While
Return x Mod c
End Function