https://www.acmicpc.net/problem/2581
소수 구하는 다른 방법인 에라토스테네스 방법도 공부해야 할듯
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
static int N, M;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
M = Integer.parseInt(br.readLine());
ArrayList<Integer> al = new ArrayList<Integer>();
int sum = 0;
for (int i = N; i <= M; i++) {
boolean flag = true;
if(i == 1)
continue;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
sum += i;
al.add(i);
}
}
Collections.sort(al);
if(al.isEmpty()) {
System.out.println("-1");
}
else {
System.out.println(sum);
System.out.println(al.remove(0));
}
}
}
'프로그래밍 & IT > Algorithm' 카테고리의 다른 글
[알고리즘] 백준 2089번 -2진수 :: 우유 (0) | 2022.03.04 |
---|---|
[알고리즘] 백준 11653번 소인수분해 :: 우유 (0) | 2021.10.20 |
[알고리즘] 백준 1978번 소수찾기 :: 우유 (0) | 2021.10.20 |
[알고리즘] - 소수 구하는 알고리즘 및 구현 (0) | 2021.10.18 |
[알고리즘] 백준 1712번 손익분기점 :: 우유 (0) | 2021.10.18 |