Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RezaT4795/854c0c63f012b6506631a991e77c48e9 to your computer and use it in GitHub Desktop.
Save RezaT4795/854c0c63f012b6506631a991e77c48e9 to your computer and use it in GitHub Desktop.
Puzzle "first 10-digit prime found in consecutive digits of e"
import math
def get_e():
from decimal import Decimal, getcontext
getcontext().prec = 200 # improve precision
e, f, n = Decimal(0), Decimal(1), Decimal(1)
while True:
old_e = e
e += Decimal(1) / f
if e == old_e:
break
f *= n
n += Decimal(1)
return Decimal(e)
def is_prime(n):
if n % 2 == 0 and n>2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
def find_prime(number=get_e(), n_prime_len=10):
if not isinstance(number, str):
s = str(number)[2:]
for i,_ in enumerate(s):
number = int(s[i:i+n_prime_len])
if is_prime(number):
print("Found prime number {} at index {}".format(i, number))
break
if __name__ == '__main__':
find_prime()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment