-3
\$\begingroup\$

This is my solution for problem 2 of Project Euler using Python:

a, b = 1, 1
total = 0
while a <= 4000000:
 if a % 2 == 0:
 total += a
 a, b = b, a+b # the real formula for Fibonacci sequence
print (total)

I put the codes in the function IsEven:

def IsEven(n):
 if n % 2 == 0:
 return True
 else:
 return False
Toby Speight
87.9k14 gold badges104 silver badges325 bronze badges
asked May 23, 2022 at 6:01
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Did you take a good look at the feedback on your previous question? \$\endgroup\$ Commented May 23, 2022 at 6:34
  • \$\begingroup\$ @Mast - Yes. I took a good look at the feedback on my previous question. Is there a problem I did not fix? \$\endgroup\$ Commented May 23, 2022 at 6:42
  • 2
    \$\begingroup\$ The question still is not self-contained; please edit to add a short summary of the problem statement. You can keep the link as a reference, but your question does need to be complete even if the linked material isn't accessible. Thanks. \$\endgroup\$ Commented May 23, 2022 at 12:12
  • 2
    \$\begingroup\$ Have you looked at other reviews of code solving this problem? Some of them even appear in the "Related" sidebar, normally on the right. \$\endgroup\$ Commented May 23, 2022 at 12:16

1 Answer 1

2
\$\begingroup\$

Modularity

We have the printing and the constraints (the constant 4000000) mixed in with the logic of the code. If we create a function, we can unit-test it, and we can time its execution (both of which will be useful to you if you attempt other Project Euler challenges). Perhaps something like

def sum_even_fibonacci(limit)
 '''Return the sum of all even Fibonacci numbers
 not greater than LIMIT
 '''
 a, b = 1, 1
 total = 0
 while a <= limit:
 if a % 2 == 0:
 total += a
 a, b = b, a+b
 return total

Simplify

The function that's unused is very long winded. Any time we write if condition: return True; else: return False, we could simply write return condition. I.e.

def IsEven(n):
 return n % 2 == 0

Algorithm

Instead of generating all the Fibonacci numbers, use your mathematical knowledge to generate just the even ones. Can you work out a formula for their sum?

answered May 23, 2022 at 12:19
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think "not greater than 'limit'" would be more appropriate in the comments "not greater than LIMIT", because name in python are case sensitive \$\endgroup\$ Commented May 23, 2022 at 17:01
  • 2
    \$\begingroup\$ I'm not really sure of conventions for Python doc-comments, but upcasing the argument names in Emacs Lisp documentation doesn't seem to cause any confusion there, and I find it does help users to quickly find where each argument is used. \$\endgroup\$ Commented May 24, 2022 at 6:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.