def is_prime(a): if a < 2: return False if a in (2, 3): return True if a % 2 == 0 or a % 3 == 0: return False # Miller-Rabin test with specific bases for 32-bit integers d, s = a - 1, 0 while d % 2 == 0: d //= 2 s += 1 for i in [2, 7, 61]: if i >= a: continue x = pow(i, d, a) if x == 1 or x == a - 1: continue for j in range(s): x = pow(x, 2, a) if x == a - 1: break else: return False return True while True: try: a, b = map(int, input().split()) c = 0 for i in range(a, b + 1): if is_prime(i): c += 1 print(c) except EOFError: break
def is_prime(a): if a < 2: return False if a in (2, 3): return True if a % 2 == 0 or a % 3 == 0: return False # Miller-Rabin test with specific bases for 32-bit integers d, s = a - 1, 0 while d % 2 == 0: d //= 2 s += 1 for i in [2, 7, 61]: if i >= a: continue x = pow(i, d, a) if x == 1 or x == a - 1: continue for j in range(s): x = pow(x, 2, a) if x == a - 1: break else: return False return True while True: try: a, b = map(int, input().split()) c = 0 for i in range(a, b + 1): if is_prime(i): c += 1 print(c) except EOFError: break
miller rabin是一種無恥快速的解法