题目要求是:

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.

这个题目提供两种方法来计算。

方法一:采用最笨拙的方式,直接从1到1000,每一个数都去看看是否可以被3或者5整除。Java实现代码如下:

1 public static long getSum(int number) {
2         long sum = 0;
3         for (int i = 1; i < number; i++) {
4             if (i % 3 == 0 || i % 5 == 0) {
5                 sum += i;
6             }
7         }
8         return sum;
9     }
方法一

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-02
  • 2021-10-30
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
  • 2021-06-18
相关资源
相似解决方案