1 Multiples of 3 and 5 - Project Euler

来源:互联网 发布:网络传真软件破解版 编辑:程序博客网 时间:2024/05/19 20:41
Multiples of 3 and 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.




package xxx.xxx.xxx;

public class MultiplesOf {


private int compute(int range, int a, int b) {
int sum = 0;
for (int i = 1; i < range; i++) {
if (i % a == 0 || i % b == 0) {
System.out.println(i);
sum = sum + i;
}
}
return sum;
}




/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MultiplesOf mul = new MultiplesOf();
System.out.println(mul.compute(1000, 3, 5));
}




}

0 0
原创粉丝点击