1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Problem001 { public static void main(String[] args) { int sum = 0 ; for(int i = 1; i < 1000; ++i) { if((i%3 == 0)||(i%5 == 0)) { sum += i; } } System.out.println("Sum:" + sum); } } | cs |
정답: 233168
실행 시간: 300000ns = 0.0003초
15의 배수의 합을 빼줘야 하는 거 아닌가 했지만, 생각해보니 아니었다.
어차피 3의 배수 또는 5의 배수일 때 더하기 때문에 중복 카운팅은 없다.
'Programming > Solutions' 카테고리의 다른 글
[Project Euler] Problem 004 (0) | 2019.03.20 |
---|---|
[Project Euler] Problem 003 (0) | 2019.03.19 |
[BAEKJOON] 2839번: 설탕 배달 (0) | 2019.03.19 |
[BAEKJOON] 1002번: 터렛 (0) | 2019.03.19 |
[Project Euler] Problem 002 (0) | 2019.03.18 |