some more python trouble
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
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
You should read this article: http://20bits.com/articles/introduction-to-dynamic-programming/
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.
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
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.
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.