-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2.py
77 lines (65 loc) · 2.21 KB
/
p2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Project Euler - Problem 2
Copyright (C) 2014 Thomas Vanesse
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
from pylab import *
def fibo_memo(n) :
'''
Returns the nth number in the fibonacci sequence.
'''
# A clean way to create a static attribute within a function
if not hasattr(fibo_memo, "memo"):
fibo_memo.memo = {}
if n in fibo_memo.memo :
return fibo_memo.memo[n]
else :
if n==0:
return 0
elif n <= 2 :
return 1
else :
f = fibo_memo(n-1) + fibo_memo(n-2)
fibo_memo.memo[n] = f
return f
def p2 (N):
'''
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms
will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values
do not exceed `N`, find the sum of the even-valued terms.
'''
# Recall that even numbers always end with a digit of 0, 2, 4, 6 or 8.
# I noticed that if you take a closer look at the Fibonacci sequence, you
# have some kind of pattern alternating between even and odd values. See for
# yourself :
#
# Fib : 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...
# index: 1 2 3 4 5 6 7 8 9 10 11 12 13 14
# even : n n y n n y n n y n n y n n
#
# But I can't explain why. Anyway I think I can only compute the nth Fibonacci
# number once every 3 step, according to the pattern above.
curr_index = 3
acc = []
while True:
f = fibo_memo(curr_index)
if f > N:
break
else:
acc.append(f)
curr_index += 3
return sum(acc)
print(p2(4000000))