def is_prime(N): if N < 2: return False for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]: if N % p == 0: return N == p d = N - 1 s = 0 while d % 2 == 0: d >>= 1 s += 1 for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]: if a >= N: continue x = pow(a, d, N) if x == 1 or x == N - 1: continue for _ in range(s - 1): x = pow(x, 2, N) if x == N - 1: break else: return False return True