-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeuler002.py
More file actions
48 lines (32 loc) · 1.02 KB
/
Copy pathpeuler002.py
File metadata and controls
48 lines (32 loc) · 1.02 KB
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
'''
Created on 3/06/2013
@author: Robert Higgins
This is intended to solve problem 001 from Project Euler.
Like problem 001 this can be solved through brute force alone.
'''
# A brute force solution for the general problem of finding the sum of the
# even fibonacci numbers with values less than limit
def brute_force(limit):
sum = 0
for i in range(0, limit):
f = fib(i)
if f >= limit: break
if f % 2 == 0: sum += f
return sum
# Finds the nth fibonacci number where n = index
def fib(index):
assert (index >= 0)
if index < 2: return index
prev = 1
value = 1
for i in range(2, index):
tmp = value
value += prev
# TODO How best to limit the potential size of the long?
prev = tmp
return value
if __name__ == '__main__':
# Problem: Find the sum of the even fibonacci numbers with values less
# than 4,000,000
print('Sum of even fibonacci numbers less than 4,000,000:', \
brute_force(4000000));