Today we tried to implement the powerful Podudoprime Rabin-Miller test.
Used Wolfram Mathworld as a reference, lines 3-5 summarize my code pretty much.
However, when I run the program, it says (sometimes) that prime numbers (even as low as 5, 7, 11) are not prime. I looked at the code for a very long time and I can not understand what is wrong.
For reference, I looked at this site in the same way as many other sites, but most of them use a different definition (maybe the same thing, but since I'm new to this math, I don't see the same obvious connection).
My code is:
import random
def RabinMiller(n, k):
if n < 2 or n % 2 == 0:
return False
if n == 2:
return True
s = 0
r = n - 1
while r % 2 == 0:
s = s + 1
r = r // 2
for i in range(k):
a = random.randrange(1, n)
if pow(a, s, n) == 1:
return True
for j in range(r):
if pow(a, 2**j*s, n) == -1 % n:
return True
return False
print(RabinMiller(7, 5))
How does this differ from the definition given in Mathworld?