3 ms·
Here's a fun way to solve Using the Python language, have the function pyChallenge() add up all of the numbers from 1 to 1000. But every time a number appears
by cr4zy 15y ago
Here's a fun way to solve
Using the Python language, have the function pyChallenge() add up all of the numbers from 1 to 1000. But every time a number appears that is divisible by 5 or 7, disregard it and do not add that number to the others (ie. disregard 7, 10, 15, 21, etc). Do not put any code outside of the function and use the print keyword to return your answer from within the function.
def pyChallenge():
sum = (1000 * (1000 + 1)) / 2
for i in range(5, 1001, 5):
sum -= i
for i in range(7, 1001, 7):
sum -= i
for i in range(35, 1001, 35):
sum += i
print sum
#As opposed to:
#sum = 0
#for i in range(1, 1000):
# if i % 5 and i % 7:
# sum += i
#print sum
# this call is needed to test your function
pyChallenge()
- mykhamill 15y agoAny particular reason you didn't go for the list comprehension? def pyChallenge(): print sum([el for el in range(1001) if not((el%5==0) or el%7==0))]) # this call is needed to test your function pyChallenge()