Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

some more python trouble


ghost's Avatar
0 0

so, ive been trying to solve another problem in python. im not sure if all you are familiar with the Fibonacci sequence, but basically its generated by adding the previous to numbers 1, 2, 3, 5, 8, …etc. so what im trying to do is find the sum of all the even number below four million the sequence part is working fine, however for the sum it always prints 0

a=1
b=2

while num < 4000000:
	sum=0
	num=a+b
	a=num
	num=a+b
	b=num
	if num%2==0:
		sum+=num
	if a%2==0:
		sum+=a
print "the sum of all the even numbers is", sum

tkearn5000's Avatar
Member
0 0

You're reinitializing sum=0 each time through the loop. Since the last fibonacci number before 4000000 is odd, sum is 0 at the end. You have to initialize sum outside of the loop.

Also, you're using more variables than you need. The code could be a little cleaner like this:

sum=0
a=1
b=1

while b < 4000000:
	if b%2==0:
		sum+=b
        a, b = b, a+b
print "the sum of all the even numbers is", sum

ghost's Avatar
0 0

that was the problem i was having before with a and b, suprised i didnt see that for sum. thanks


ghost's Avatar
0 0

Whats the the fibonnachi sequence have to do with the sum of all even numbers under 4million?

This is how the fibonnachi sequences should be done in python… code bank I can't even work out how your code works, it may be my stupidity but I have a feeling it's just terrible code try and keep it simple.


tkearn5000's Avatar
Member
0 0

I'm pretty sure this is a question from Project Euler. Find the sum of all even numbers in the fibonacci sequence below 4000000.


richohealey's Avatar
Python Ninja
0 0
#!/usr/bin/python
a, b = 0 1
while 1:
    print a
    a += b
    a, b = a, b
    if a < 40000: break

NEXT!

ps you can do it with recursion, map() and self referencing lambda's if you want to try to make your own head explode.

EDIT: Saw that it's homework, sample code broken. Simple/easy fix though.


ynori7's Avatar
Future Emperor of Earth
0 0

richohealey wrote:

#!/usr/bin/python
a, b = 0 1
while 1:
    print a
    a += b
    a, b = a, b
    if a < 40000: break

Your code is pretty wrong. Missing comma in the first line, "a,b=a,b" does nothing, and since 'a' starts out less than 40000 it will break after one iteration.


ghost's Avatar
0 0

wolfmankurd wrote: Whats the the fibonnachi sequence have to do with the sum of all even numbers under 4million?

This is how the fibonnachi sequences should be done in python… code bank I can't even work out how your code works, it may be my stupidity but I have a feeling it's just terrible code try and keep it simple.

First the article, I was half drunk and posted that while reading Fibonacci in his post, so it really doesnt apply as much. Yet the sequence he was calculating did involve the Fibonacci sequence.

Ie it wasnt 2+4+6+…+4000000 It was 2+8+34+….

Interms of your next comment.

How can you say that yours is so much better when its almost the exact code? The only difference is that you have to loop 2 extra times. So if you want to get technical the fib2 way IS better then what you have shown.

Here I took yours from the code bank and made one minor modification to it so that it doesnt print all numbers only the final one you want, and put it into a function.

#!/usr/bin/python

def fib2(n):
    n2, n1 = 0, 1
    for i in range(n-2):
        n2, n1 = n1, n1 + n2
    return n2+n1

def wolfs_fib(n):
    a = 0
    b = 1
    for _ in range(n):
        a, b = b, a+b
    return a
 
print wolfs_fib(100)
print fib2(100)

EDIT: GODDAMNIT MISINTERPRETATION


tkearn5000's Avatar
Member
0 0

Actually that article is very helpful. I've seen it before, and learned a few new things with a second reading. Solving the above question (Find the sum of all even fibonacci numbers below 4000000) involves a few slight modifications to the code in the article, but the underlying algorithm for calculating fibonacci numbers is still there. For example, rather than calculating n fibonacci numbers, my code calculate all of the fibonacci numbers less than 4000000. At each step, I check to see if the new number I have found is even, if so I increment the sum, then I calculate the next fibonacci number in the same way the article does.

The OP could learn a lot from that article as well, since his code had numerous errors not only in the way he was summing the even numbers, but in the way he was attempting to calculate the fibonacci sequence.


ghost's Avatar
0 0

stdio wrote: How can you say that yours is so much better when its almost the exact code? The only difference is that you have to loop 2 extra times. So if you want to get technical the fib2 way IS better then what you have shown.

Here I took yours from the code bank and made one minor modification to it so that it doesnt print all numbers only the final one you want, and put it into a function.

I don't understand your criticism, the code is idiomatic.

Ah lol! I see, you think I read the article you coded, and said it was bad code? I didn't read the article you posted and I thought it was fairly obvious my comment was directed at OP, This is OP's thread after all.

edit: also, if forced to compare the two code snippets I still reckon mine edges ahead. First single line assignments I think I no nos. And the use of _ especially for throw away variables is better than a named one even if it's only i. But thats just anal nit picking.


ghost's Avatar
0 0

AHHHH edited.