[READ-ONLY] Mirror of https://github.com/mrgnw/python-scripts.
0

Configure Feed

Select the types of activity you want to include in your feed.

python-scripts / fibonacci_evens.py
685 B 41 lines
1def is_even(n): 2 if n%2 == 0: 3 return True 4 else: 5 return False 6 7def evens(list): 8 new_list = [] 9 for i in list: 10 if is_even(i): 11 new_list.append(i) 12 return new_list 13 14def fib_helper(n): 15 if n == 0: 16 return 0 17 else: 18 return fib_improved(n, 0, 1) 19 20 21def fib_improved(n, p0, p1): 22 if n == 1: 23 return p1 24 else: 25 return fib_improved(n-1, p1, p0 + p1) 26 27 28def fibs_under(x): 29 fibs = [] 30 31 count = 2 32 while fib_helper(count) < x: 33 #print fib_helper(count) 34 fibs.append(fib_helper(count)) 35 count += 1 36 37 return fibs 38 39#print fib_helper(18) 40 41print sum(evens(fibs_under(4000000)))