Prime Gaps & Twin Primes
Prime Gaps
A prime gap is the difference between two consecutive primes and where is the next prime after :
The smallest possible gap is 2 (since consecutive integers differ by 1, and at least one must be even, so consecutive primes must differ by at least 2 — except for the pair (2, 3)).
Twin Primes
Two primes are called twin primes — the smallest possible gap between odd primes. Examples: , , , , , …
The Twin Prime Conjecture (unsolved!) states there are infinitely many twin prime pairs.
def is_prime(n):
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0: return False
return True
def next_prime(n):
candidate = n + 1
while not is_prime(candidate):
candidate += 1
return candidate
def prime_gap(p):
return next_prime(p) - p
def is_twin_prime(p):
return is_prime(p) and prime_gap(p) == 2
print(prime_gap(11)) # 2 (next prime is 13)
print(is_twin_prime(11)) # True
print(prime_gap(7)) # 4 (next prime is 11)
print(is_twin_prime(7)) # False
Maximal Gaps
Prime gaps grow on average (by the Prime Number Theorem), but they are irregular. After , the next prime is — a gap of 6. After , the next is — a gap of 8.
Polignac's Conjecture
Every even number occurs as a prime gap infinitely often. This is unsolved for every specific even value, including 2 (the Twin Prime Conjecture).
Your Task
Using the is_prime and next_prime helpers provided in the starter code, implement:
prime_gap(p): gap from to the next primeis_twin_prime(p): whether is the smaller of a twin prime pair