codewars(python)練習筆記十六:求出小于solution的能整除3或5的數(shù)的和
題目
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once.
Courtesy of ProjectEuler.net
題目大意:
求出小于solution的能整除3或5的數(shù)的和。
我的解法:
#!/usr/bin/python
def solution(number):
temp = 0
for item in range(0,number):
if item%3 == 0 or item%5 == 0:
temp += item
return temp
print solution(10)
簡化解法:
def solution(number):
return sum([x for x in range(number) if x % 3 == 0 or x % 5 == 0])
牛逼解法:
def solution(number):
a3 = (number-1)/3
a5 = (number-1)/5
a15 = (number-1)/15
result = (a3*(a3+1)/2)*3 + (a5*(a5+1)/2)*5 - (a15*(a15+1)/2)*15
return result
這個一開始沒看明白俏险。但后來轉念一想就明白了:這個就是求出能被3整除的數(shù)的和疫诽,加上能被5整除的數(shù)的和补履,減去能被15整除的數(shù)的和决侈,就滿足題目要求了史飞。