Here is a way to do it:
Generate 1st 1000 digits of e using continued fractions method with answer by @quantum in Code to Generate e one Digit at a Time, which is from answer by @wnoise in Generating digits of square root of 2, which is an "adaptation of Haskell code ... that has been floating around":
def z(contfrac, a=1, b=0, c=0, d=1):
for x in contfrac:
while a > 0 and b > 0 and c > 0 and d > 0:
t = a // c
t2 = b // d
if not t == t2:
break
yield t
a = (10 * (a - c*t))
b = (10 * (b - d*t))
# continue with same fraction, don't pull new x
a, b = x*a+b, a
c, d = x*c+d, c
for digit in rdigits(a, c):
yield digit
def rdigits(p, q):
while p > 0:
if p > q:
d = p // q
p = p - q * d
else:
d = (10 * p) // q
p = 10 * p - q * d
yield d
def e_cf_expansion():
yield 1
k = 0
while True:
yield k
k += 2
yield 1
yield 1
def e_dec():
return z(e_cf_expansion())
gen = e_dec()
e = [str(gen.next()) for i in xrange(1000)]
e.insert(1, '.')
Function to test primality of an integer selected for efficiency from Rosetta Code Primality_by_trial_division#Python:
def isprime(a):
if a < 2: return False
if a == 2 or a == 3: return True # manually test 2 and 3
if a % 2 == 0 or a % 3 == 0: return False # exclude multiples of 2 and 3
maxDivisor = a**0.5
d, i = 5, 2
while d <= maxDivisor:
if a % d == 0: return False
d += i
i = 6 - i # this modifies 2 into 4 and viceversa
return True
Find the first 10 digit prime in e (my contribution):
for i in range(len(e[2:])-10):
x = int(reduce(operator.add,e[2:][i:i+10]))
if isprime(x):
print x
print i
break
This prints:
7427466391
98
Meaning that the first 10 digit prime in e occurs in the 98th postion after the decimal point in agreement with http://explorepdx.com/firsten.html under 'The location of the answer'.
A simpler way to generate digits of e is with Euler's series expansion which can be done as follows with code adapted from Euler's Number with 100 Digit Precision (Python) that uses Python's Decimal class for adequate precision:
import operator
import decimal as dc
def edigits(p):
dc.getcontext().prec = p
factorial = 1
euler = 2
for x in range(2, 150):
factorial *= x
euler += dc.Decimal(str(1.0))/dc.Decimal(str(factorial))
return euler
estring = edigits(150).to_eng_string()[2:]
for i in range(len(estring)-10):
x = int(reduce(operator.add,estring[i:i+10]))
if isprime(x):
print x
print i
break
This prints:
7427466391
98
As pointed out by @MarkDickinson an even easier method is to use the decimal module directly to generate e with the necessary precision. For example:
import operator
import decimal
decimal.getcontext().prec = 150
e_from_decimal = decimal.Decimal(1).exp().to_eng_string()[2:]
for i in range(len(e_from_decimal)-10):
x = int(reduce(operator.add,e_from_decimal[i:i+10]))
if isprime(x):
print x
print i
break
This prints:
7427466391
98