This is my solution for problem 1 of the projectEuler with python:

import time
start_time = time.time()   #Time at the start of program execution

def multiple_3_or_5(n):
    if n % 3 ==0 or n % 5 ==0:
        return True
        return False

sum = 0
for i in range (1,1000):
    # print ("checking :" , i)
    if multiple_3_or_5(i):
        # print ("multiple is fine for", i)
        sum = sum + i
        # print ("Sum is =", sum)

print (sum)

end_time = time.time()   #Time at the end of execution
print ("Time of program execution:", (end_time - start_time))   #Time of program execution

  • \$\begingroup\$ This site is all about getting answers. And what do you ask for? \$\endgroup\$
    – JosefZ
    May 22, 2022 at 16:46
  • 3
    \$\begingroup\$ @JosefZ, on Code Review, every question is "how could my code be improved?" Unlike other SE sites, that doesn't need to be explicitly articulated here. \$\endgroup\$ May 23, 2022 at 6:07
  • \$\begingroup\$ @JosefZ - Thanks for your explanation \$\endgroup\$ May 23, 2022 at 7:04

1 Answer 1


Put all of your code in functions. Including the code to represent Euler problem 1, along with the code to exercise that function.

Implement automated tests for those functions. While implementing such code, express your expections for correctness in the form of automated tests. Ultimately, you'll want to learn how to use one of Python's testing frameworks, such as pytest. In the meantime, or in small-scale or informal situations, you can roll your own testing code, as illustrated below.

Functions based on a simple boolean test can return directly. You can drop the if-else clauses from multiple_3_or_5() and just return the boolean result.

The built-in sum function takes an iterable. Just add up the values of i for which multiple_3_or_5(i) returns true.

Don't obsess over performance until you know you have a problem. This program is uninteresting from a performance perspective. Drop the extraneous timing code. And even if you disagree with my perspective, at least focus your measurements on the relevant code -- just the call of euler_1() function, rather than the execution of the entire program.

def main():
    TESTS = (
        (10, 23),
        (1000, 233168),
    for limit, expected in TESTS:
        got = euler_1(limit)
        if got == expected:
            print(limit, got, expected)

def euler_1(limit = 1000):
    return sum(i for i in range (1, limit) if multiple_3_or_5(i))

def multiple_3_or_5(n):
    return n % 3 == 0 or n % 5 == 0

if __name__ == '__main__':

  • 2
    \$\begingroup\$ "This program is uninteresting from a performance perspective" - even more so when the closed-form solution (without the loop) is used! And that's the kind of thinking that Project Euler aims to test, rather than simply coding the obvious poorly-scaling way... \$\endgroup\$ May 22, 2022 at 19:23
  • 1
    \$\begingroup\$ @TobySpeight Indeed. I probably should emphasize that my disinterest is conditional on limit = 1000. Not so much in question #1, but eventually Project Euler demands the kind of math-centric thinking that you reference: solving problems by computing the answer nearly-immediately rather than brute-forcing it. Question #1 is sort of the warm up: get your functional and testing infrastructure set up for success, so that you you survive in later rounds. \$\endgroup\$
    – FMc
    May 22, 2022 at 19:40
  • \$\begingroup\$ It should also be explicitly pointed out that OP was overwriting the builtin sum function. \$\endgroup\$
    – Teepeemm
    May 22, 2022 at 21:03
  • \$\begingroup\$ @TobySpeight - It is a good thing that you said that problem-solving thinking is important in Euler's problems. I am also correcting this thinking. \$\endgroup\$ May 23, 2022 at 4:51
  • \$\begingroup\$ @FMc - Exactly right. Project Euler: "solving problems by computing the answer nearly-immediately" \$\endgroup\$ May 23, 2022 at 4:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.