Last active
August 12, 2021 13:50
-
-
Save mnp/af01656b5362291cba674d17079fd28e to your computer and use it in GitHub Desktop.
Find cases where twin primes lead to more twin primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# see https://old.reddit.com/r/mathematics/comments/p2wv1a/twin_primes_square_distances/ | |
def find_prime_pairs(n): | |
sieve = [True] * n | |
if n > 0: | |
sieve[0] = False | |
if n > 1: | |
sieve[1] = False | |
for number in range(2, int(n ** 0.5) + 1): | |
if sieve[number]: | |
for index in range(number * number, n, number): | |
sieve[index] = False | |
return [(a, b) for b, a in enumerate(range(0, n - 2), start=2) if sieve[a] and sieve[b]] | |
def is_prime(n): | |
if n == 2 or n == 3: return True | |
if n < 2 or n%2 == 0: return False | |
if n < 9: return True | |
if n%3 == 0: return False | |
r = int(n**0.5) | |
f = 5 | |
while f <= r: | |
if n % f == 0: return False | |
if n % (f+2) == 0: return False | |
f += 6 | |
return True | |
def foo(n): | |
m = n/6 | |
return int(m*m*6) | |
for i in find_prime_pairs(100000): | |
z = foo(i[0] + 1) | |
if is_prime(z-1) and is_prime(z+1): | |
print(f'{i} -> ({z-1}, {z+1})') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment