Project Euler 1
Project Euler 1
题目
Multiples of \(3\) or \(5\)
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\)的倍数进行求和。加起来后,将\(15\)的倍数的总和减去即可。
代码
1 | N = 1000 |