Author Topic: [Python] [spoiler] Project Euler Question Two- Critique Please  (Read 693 times)

0 Members and 1 Guest are viewing this topic.

Offline AnonymousCelt

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
    • View Profile

Task is to return the sum of all even fibonacci numbers below 4000000.

Code: [Select]

def ProblemTwo():

last = 1
current = 1
temp = 2
tally = 0

while current < 4000000:
temp = last + current
last = current
current = temp
if current % 2 == 0:
tally += current

print "My Answer is: %d" % tally




def main():
ProblemTwo()


if __name__ == '__main__':
main()


Any feedback is greatly appreciated.

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: [Python] [spoiler] Project Euler Question Two- Critique Please
« Reply #1 on: March 23, 2015, 11:07:45 am »
Not wrong perse but why do you use a while and not a for loop ?
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline AnonymousCelt

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
    • View Profile
Re: [Python] [spoiler] Project Euler Question Two- Critique Please
« Reply #2 on: March 23, 2015, 11:25:51 am »
Not wrong perse but why do you use a while and not a for loop ?
To be honest, because I was causing iteration within the loop and the variable was already defined I thought it would be best using the while loop as it only needs the check added.
Might be different in Python though (came from Java).

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: [Python] [spoiler] Project Euler Question Two- Critique Please
« Reply #3 on: March 23, 2015, 11:32:07 am »
Well tell you what you should really read about the numbers before coding it out. Bruteforcing isn't a good practice. You should carefully observe the stuff.

I will give you a simple hint with which you can improve your code.

Lets examine the Fibo series :-

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, .....


Do you observe something ?

What we observe is that every third number is even.

Well that's your hint for now.
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline AnonymousCelt

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
    • View Profile
Re: [Python] [spoiler] Project Euler Question Two- Critique Please
« Reply #4 on: March 23, 2015, 08:16:58 pm »
Well tell you what you should really read about the numbers before coding it out. Bruteforcing isn't a good practice. You should carefully observe the stuff.

I will give you a simple hint with which you can improve your code.

Lets examine the Fibo series :-

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, .....


Do you observe something ?

What we observe is that every third number is even.

Well that's your hint for now.


Ah, I get it.
For loop incrementing the fib sequence by 3 at a time, adding to the tally/total avoiding the need to verify whether it is even or not.
I posted this before receiving your feedback in the first project euler question. I'll look back into it all for a while before doing any more as I clearly have more to learn before I can benefit fully from the exercises.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: [Python] [spoiler] Project Euler Question Two- Critique Please
« Reply #5 on: March 23, 2015, 08:30:18 pm »
Alright. I will try to post some detailed answer on this. Maybe tomorrow.
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins