Join us at IRC!
Capitalism is an Island of wealth in a sea of poverty
Friday, May 25, 2012
Navigation
Members Online
Total Online: 36
Web Spiders: 14
Guests Online: 32
Members Online: 4

Registered Members: 70214
Newest Member: cyrusx
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

some more python trouble

iantharan
Member



Posts: 27
Location: right behind you...
Joined: 14.04.08
Rank:
Hacker Level 3
Posted on 16-01-10 17:42
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
num=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
Author

RE: some more python trouble

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
God
Posted on 16-01-10 17:57
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






Edited by tkearn5000 on 16-01-10 18:00
Author

RE: some more python trouble

iantharan
Member



Posts: 27
Location: right behind you...
Joined: 14.04.08
Rank:
Hacker Level 3
Posted on 17-01-10 11:05
that was the problem i was having before with a and b, suprised i didnt see that for sum. thanks
Author

RE: some more python trouble

stdio
Member

Posts: 375
Location: omnipresent
Joined: 06.04.08
Rank:
God
Posted on 17-01-10 13:39
You should read this article:
http://20bits.com/articles/introduction-to-dynamic-programming/






I'm sorry, I cant hear you over the sound of how awesome I am!
www.thewebsiteisdown.com
Author

RE: some more python trouble

wolfmankurd
Member



Posts: 1519
Location: UK
Joined: 30.05.05
Rank:
God
Posted on 17-01-10 21:25
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.


BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.




Edited by wolfmankurd on 17-01-10 21:26
Widowmakr@hotmail.com http://LetsHackStuff.com
Author

RE: some more python trouble

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
God
Posted on 17-01-10 21:58
I'm pretty sure this is a question from Project Euler. Find the sum of all even numbers in the fibonacci sequence below 4000000.



Author

RE: some more python trouble

richohealey
Python Ninja



Posts: 1020
Location: #!/usr/local/bin/python
Joined: 01.05.06
Rank:
Ninja
Posted on 18-01-10 03:45

#!/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.


blog.psych0tik.net


Nice one R3l3ntl3ss^^


Edited by richohealey on 18-01-10 03:50
bitchohealey at hotmail dot com skype:richohealey www.psych0tik.net
Author

RE: some more python trouble

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 18-01-10 04:05
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.




ynori7 http://halls-of-valhalla.org
Author

RE: some more python trouble

stdio
Member

Posts: 375
Location: omnipresent
Joined: 06.04.08
Rank:
God
Posted on 18-01-10 06:31
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



I'm sorry, I cant hear you over the sound of how awesome I am!


Edited by stdio on 18-01-10 13:18
www.thewebsiteisdown.com
Author

RE: some more python trouble

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
God
Posted on 18-01-10 12:00
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.





Edited by tkearn5000 on 18-01-10 12:20
Author

RE: some more python trouble

wolfmankurd
Member



Posts: 1519
Location: UK
Joined: 30.05.05
Rank:
God
Posted on 18-01-10 13:05
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.


BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.




Edited by wolfmankurd on 18-01-10 16:34
Widowmakr@hotmail.com http://LetsHackStuff.com
Author

RE: some more python trouble

stdio
Member

Posts: 375
Location: omnipresent
Joined: 06.04.08
Rank:
God
Posted on 18-01-10 13:16
AHHHH edited.



I'm sorry, I cant hear you over the sound of how awesome I am!


Edited by stdio on 18-01-10 13:16
www.thewebsiteisdown.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.