Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Find sum of even numbers in Fibonacchi seq

Name: Anonymous 2013-10-15 2:46

This is the problem: https://projecteuler.net/problem=2

This is my (probably absolutely horrible) way of doing this in python:


def fib(i):
    if i == 0:
        return 0
    elif i == 1:
        return 1
    else:
        return fib(i-1) + fib(i-2)

array0 = []
array1 = []

for i in range(0, 50):
    array0.append(fib(i))
 
for i in array0:
    if i % 2 == 0:
        print '%d even' % i
        array1.append(i)
    else:
        print '%d odd' % i

print sum(array1)


I have only tested this with the first 40 numbers in the Fibonacci sequence, I cannot seem to get it to interpret anything much bigger. I've only left it on for like 10 minutes though when trying to find the first 100 and came back and it was still interpreting it. I think it froze but I can't tell.

How the HELL does this work btw:

return fib(i-1) + fib(i-2)

This shit is just a complete mindfuck to me.

Name: Anonymous 2013-10-15 21:32

>>15
but isn't that exactly how sicp tells you to code fibs? lol

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List