# Project Euler - Problem #1 - Multiples of 3 or 5 - Python

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
else:
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


• This site is all about getting answers. And what do you ask for? May 22 at 16:46
• @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. May 23 at 6:07
• @JosefZ - Thanks for your explanation May 23 at 7:04

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('ok')
else:
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__':
main()


• "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... May 22 at 19:23
• @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.
– FMc
May 22 at 19:40
• It should also be explicitly pointed out that OP was overwriting the builtin sum function. May 22 at 21:03
• @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. May 23 at 4:51
• @FMc - Exactly right. Project Euler: "solving problems by computing the answer nearly-immediately" May 23 at 4:54