I expect you made a typo. You don't want (i%35)==0
, you want (i%5)==0
.
The PEP-8 style guide for Python requires 1 space before and after operators, and after commas. Use PyLint or equivalent tool to ensure you follow all of the PEP-8 guidelines.
check_sum
is unused, and can be omitted.
The brackets around the if( ... ):
condition are unnecessary. This is Python, not C, C++ or Java:
if (i % 3) == 0 or (i % 5) == 0: #divisor condition
There is no need to create a list just to add up all the numbers after the fact. You are only using each value once, so you could simply add the numbers up as you find them:
def prob_1():
sum_of_multiples = 0
for i in range(1, 1000): # Take numbers up to but not including 1000
if (i % 3) == 0 or (i % 5) == 0: #divisor condition
sum_of_multiples += i
return sum_of_multiples
You should add """doc_strings"""
to your functions:
def prob_1():
"""
Compute the sum of all the multiples of 3 or 5 below 1000.
Returns:
The sum of the multiples of 3 or 5, below 1000.
"""
sum_of_multiples = 0
for i in range(1, 1000): # Take numbers up to but not including 1000
if (i % 3) == 0 or (i % 5) == 0: #divisor condition
sum_of_multiples += i
return sum_of_multiples
You can use list comprehension a generator expression (thanks @Graipher) and the sum(...)
function to compute the result, without ever creating the list in memory:
def prob_1():
"""
Compute the sum of all the multiples of 3 or 5 below 1000.
Returns:
The sum of the multiples of 3 or 5, below 1000.
"""
return sum(i for i in range(1000) if i % 3 == 0 or i % 5 == 0)
You can also solve this problem by hand with a pen, a sheet of paper, a calculator and about 1 minute of your time. A program is entirely unnecessary.
i % 35
really the condition you would like to check? \$\endgroup\$