Euler Solution 65

From ProgSoc Wiki

Jump to: navigation, search

Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.

Solutions for Problem 65

Python by Althalus

Runtime: 4ms

limit=100

#e = [2; 1,2,1, 1,4,1, 1,6,1, ..., 1,2k,1...]
#Let's start off by gettingthe first 99 parts of our repeated fraction.
es = [2,]
count=1
#our first convergent won't be in our list es.
while len(es) < limit:
 es+=[1,2*count,1]
 count+=1
es.reverse()
fraction = [1,es[0]]
#evaluate....
for e in es[1:]:
 fraction[0],fraction[1]=fraction[1],fraction[0]
 fraction[0]+=e*fraction[1] 
print sum([int(x) for x in str(fraction[0])])
Personal tools