我解決項目歐拉練習1:爲什麼列表的總數不正確?
如果我們在下面列出10是3或 5的倍數的所有自然數,我們得到了3,5,6和9,這些倍數的總和23.找到低於1000
我一直停留在這一段時間,做一些研究,這是如何計算的總和後的 之和的3或5的所有倍數。
# range of numbers 0 to 1000
# iterate through numbers
for number in range(1001):
multiples = []
# multiples of 3 or 5
if (number % 3 == 0) or (number % 5 == 0):
# store num into array
multiples.append(number)
# add up all elements in array
total = sum(multiples)
print total
我的輸出爲1000
更新:我剛纔讀的項目歐拉不要在網上發佈的解決方案。我想我的問題與解決方案非常接近。如果主持人認爲它應該被刪除,那麼請這樣做。
在循環外採用'multiples'。 – Akavall 2014-09-27 02:47:11
閱讀此應該幫助:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – 2014-09-27 02:49:56