반응형

[문제]
10미만의 자연수에서 3과 5의 배수를 구하면 3,5,6,9이다. 이들의 총합은 23이다.
1000미만의 자연수에서 3,5의 배수의 총합을 구하라.
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.
[정답]
var result = 0;
for( var i=1; i<1000; i++ ){
if( i%3 == 0 || i%5 == 0 )
// i를 3로 나눈 값과 5로 나눈 값의 나머지가 둘다 0일 경우 true.
result += i;
}
console.log(result);
Multiples.js
0.00MB
반응형