pythonmathexpgmpy

(the first 10-digit prime in e).com python google challenge 2004


I just come up to one of challenges that claimed to be used by google 2004

(the first 10-digit prime in e).com 

independent from that, I wanted to take the challenge and solve it with python

>>> '%0.52f' % math.exp(1)
'2.71828182845904509079**5598298427**6488423347473144531250'
>>> '%0.52f' % numpy.exp(1)
'2.71828182845904509079**5598298427**6488423347473144531250'

my program returned 5598298427 which is a prime number

after looking on the internet the right answer was7427466391

but the exp number in python doesn't include that digits as you can see above

import numpy
import math

def prime(a):
    if a == 2: return True
    if a % 2 == 0: return False
    if a < 2: return False
    i = 2
    n = math.sqrt(a) + 1
    while(i < n):
        if a % i == 0:
            return False
        i += 1
    return True

def prime_e():
    e = '%0.51f' % math.exp(1)
    e = e.replace("2.","")
    for i in range(len(e)):
        x = int(e[i:10+i])
        if prime(x):
            return [i, x]

print prime_e()

so am I doing something wrong ?


EDIT: using gmpy2

def exp():
    with gmpy2.local_context(gmpy2.context(), precision=100) as ctx:
        ctx.precision += 1000
        return gmpy2.exp(1)

returns 7427466391 after 99 iterations


Solution

  • Actual e (Euler constant) value is

    http://www.gutenberg.org/files/127/127.txt

    2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630...

    and so the right answer for the challenge is 7427466391. You can't compute e with requiered precision by math.exp(1)