codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#Project Euler Problem 2: # #Find the sum of all the even-valued terms in the sequence # which do not exceed four million. f_n_minus_2 = 1 f_n_minus_1 = 2 f_n = 0 even_fibonaccis = [f_n_minus_1] for n in range(3,4000000): f_n = f_n_minus_1 + f_n_minus_2 #For this iteration: n = n-1 + n-2 #Stop when f_n greater than 4million if f_n > 4000000: break if f_n % 2 == 0: even_fibonaccis.append(f_n) f_n_minus_2 = f_n_minus_1 #For the next iteration: n-2 = n-1 f_n_minus_1 = f_n #For the next iteration: n-1 = n #Finally, sum the list. print "n =", n print even_fibonaccis print "Sum of even Fibonacchi's:", sum(even_fibonaccis)
Private
[
?
]
Run code
Submit