我的欧拉工程之路_1

Multiples of 3 and 5

Problem 1

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的倍数

问题 1

如果我们列出所有10以下的且是3或5的倍数的自然数,他们是3,5,6和9.他们的和是23.

求1000以下3或5的倍数的和

解题思路

循环变量i从3起递增至999,步进为1,判断若为3或5的倍数则让sum加上此值,sum初始为0.

//以下是用于解决第一题的JAVA程序
public class Euler1
{
    public static void main(String[] args)
    {
        int sum=0;
    for(int i=3;i<1000;i++)
    {
        if(i%3==0 || i%5==0)
        sum+=i;
    }
    System.out.println("sum="+sum);
    }
}


你可能感兴趣的:(欧拉工程,数学问题)