Multiples of 3 and 5
Question
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.
Find the sum of all the multiples of 3 or 5 below 1000.
思路
- 暴力列举所有包含3,5因子的数,进行求和
- 1000以内以3,5为因子的数的和,即考虑能被3,5各自整除的数的和,再去除能被15整除的数即可。
Python 代码
暴力列举法
1 | def sum_of_multiples1(n): |
233168
公式法
1 | def sum_of_multiples(n): |
333 199 66
233168