Follow us on Twitter!
Don't judge the unknown - Grindordie
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 23
Guests Online: 21
Members Online: 2

Registered Members: 82905
Newest Member: BLckLIght
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

some more python trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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
Code
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:
Newbie
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:
Code

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






img7.imageshack.us/img7/6588/1993.png

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

RE: some more python trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 17-01-10 13:39
You should read this article:
http://20bits.com. . .ogramming/





Author

RE: some more python trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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.




Edited by on 17-01-10 21:26
Author

RE: some more python trouble

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
Newbie
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.


img7.imageshack.us/img7/6588/1993.png
Author

RE: some more python trouble

richohealey
Member



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

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




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
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 18-01-10 04:05
richohealey wrote:
Code

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


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: some more python trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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.

Code

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




Edited by on 18-01-10 13:18
Author

RE: some more python trouble

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
Newbie
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.


img7.imageshack.us/img7/6588/1993.png

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

RE: some more python trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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.




Edited by on 18-01-10 16:34
Author

RE: some more python trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 18-01-10 13:16
AHHHH edited.




Edited by on 18-01-10 13:16