If you just want to determine if a number is prime instead of finding all the factors, the second chunk of code I posted is faster than the first. Here they are with some speed tests. The first one takes about 1 second, the second one takes about 250-300ms:
Code: Select all
#NoEnv
SetBatchLines, -1
SetFormat, Float, 0.0
n := 6746328388801
Tick := A_TickCount
f := 2, x := Sqrt(n)
while, (f <= x) {
if (!Mod(n, f))
factors .= f "`n" (n / f) "`n"
f++
}
Sort, factors, N U
factors := !factors ? "Prime" : factors
Tick := A_TickCount - Tick
MsgBox, % factors "`nComputed in " Tick "ms."
Code: Select all
#NoEnv
SetBatchLines, -1
Tick := A_TickCount
p := IsPrime(6746328388801)
Tick := A_TickCount - Tick
if (p)
MsgBox, % "The number is prime.`nComputed in " Tick "ms."
else
MsgBox, % "The number is not prime.`nComputed in " Tick "ms."
return
IsPrime(n) {
if (n < 2)
return, 0
else if (n < 4)
return, 1
else if (!Mod(n, 2))
return, 0
else if (n < 9)
return 1
else if (!Mod(n, 3))
return, 0
else {
r := Floor(Sqrt(n))
f := 5
while (f <= r) {
if (!Mod(n, f))
return, 0
if (!Mod(n, (f + 2)))
return, 0
f += 6
}
return, 1
}
}
Reference: https://en.wikipedia.org/wiki/Primality ... ve_methods