Friday, 11th September, 2009

Project Euler Problem 65

The square root of 2 can be written as an infinite continued fraction.
√2 = 1 +
1

2 +
1

2 +
1

2 +
1

2 + …

The infinite continued fraction can be written, √2 = [1;(2)], (2) indicates that 2 repeats ad infinitum. In a similar way, √23 = [4;(1,3,1,8)].

It turns out that the sequence of partial values of continued fractions for square roots provide the best rational approximations. Let us consider the convergents for √2.
1 +
1

= 3/2

2

1 +
1

= 7/5
2 +
1

2

1 +
1

= 17/12
2 +
1

2 +
1

2

1 +
1

= 41/29
2 +
1

2 +
1

2 +
1

2

Hence the sequence of the first ten convergents for √2 are:
1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, …

What is most surprising is that the important mathematical constant,
e = [2; 1,2,1, 1,4,1, 1,6,1 , … , 1,2k,1, …].

The first ten terms in the sequence of convergents for e are:
2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, …

The sum of digits in the numerator of the 10^(th) convergent is 1+4+5+7=17.

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

In order to compute this, we first must create a way of generating a representation of the continued fraction to the desired accuracy. We need to create [2; 1,2,1,1,4,1,1,6,1,1,8,1,1,16 …]. A good way to do this would be a list comprehension [x for x in e_fraction_representation(desired_limit)]. We just need that function. There’s obviously a pattern, so we can easily code this up.

def representation_generator(limit):
    clicker = 0
    for i in xrange(limit):
        if ((i +2) % 3 == 0):
             clicker += 2
             yield clicker
        else:
             yield 1

Using this alone gives

>>> [x for x in representation_generator(100)]
[1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, 14, 1, 1, 16, 1, 1, 18, 1, 1, 20, 1, 1, 22, 1, 1, 24, 1, 1, 26, 1, 1, 28, 1, 1, 30, 1, 1, 32, 1, 1, 34, 1, 1, 36, 1, 1, 38, 1, 1, 40, 1, 1, 42, 1, 1, 44, 1, 1, 46, 1, 1, 48, 1, 1, 50, 1, 1, 52, 1, 1, 54, 1, 1, 56, 1, 1, 58, 1, 1, 60, 1, 1, 62, 1, 1, 64, 1, 1, 66, 1]

Looks to be right.

In order to compute e, we have to walk along this backwards, starting at the inner-most fraction, and evaluating it, then stepping back up to the next one and continuing until we have consumed the whole list.

import fractions
	
def representation_generator(limit):
    clicker = 0
    for i in xrange(limit):
        if ((i +2) % 3 == 0):
             clicker += 2
             yield clicker
        else:
             yield 1
	
repr = [x for x in representation_generator(99)] ## The representation of necessary length
	
repr.reverse() ## Reverse it
prev = fractions.Fraction(0, 1); ## The first thunk. It's 0/1 so as not to modify the final answer
for i in repr:
    temp = i+prev
    thunk = (1/temp)
    prev=thunk
	
thunk += fractions.Fraction(2,1) ## Finalize the thunk, with the addition of 2.
print sum([int(x) for x in str(thunk.numerator)]) ## sum up the digits

Quite simple in Python. :-)

The word “thunk” is borrowed from Haskell terminology (I don’t know where it came from before then) to mean a ‘partially computed value’ or ‘partially evaluated value.’ Though I don’t think this is exactly true for how I’m using it, it seemed to fit at the time.

Done! :-D