import random import psimp as p def MillerRabinRandom(n, k): """Carry out a Miller-Rabin test on n with k iterations, choosing random values for a. """ r, d = p.trd(n-1) for iter in range(k): a = random.randint(2, n - 2) x = pow(a, d, n) if x == 1 or x == n - 1: continue compo = True for iterb in range(r - 1): x = pow(x, 2, n) if x == n - 1: compo = False break if compo: return True return False def MillerRabin(n, base): """Carry out a Miller-Rabin test on n with a specified base. """ r, d = p.trd(n-1) a = base x = pow(a, d, n) if x == 1 or x == n - 1: return False for iterb in range(r - 1): x = pow(x, 2, n) if x == n - 1: return False return True # Print pseudoprimes greater than 100 for each base, from base 1 to 15 for base in range(2, 16): print("{}:".format(base)) for i in range(2, 10000): if i % 2 == 0 or base % i == 0: continue if MillerRabin(i, base) == (p.hund_div(i) == 0): print(i, p.hund_div(i), MillerRabin(i, base)) print() MillerRabin(28, 3) print(pow(3, 27, 28)) print(pow(3, 27)) print(pow(3, 27) % 28)