본문 바로가기
Java/알고리즘

[Java] 백준 2581번 : 소수

by EricJeong 2019. 9. 5.

 

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int M = in.nextInt();
		int N = in.nextInt();
		
		int[] nums = new int[N+1];
		
		for(int i=2; i<=N; i++) {
			nums[i] = i;
		}
		
		for(int i=2; i<=N; i++) {
			if(nums[i] == 0) {
				continue;
			}
			int temp = i;
			for(int j = i+1; j<=N; j++) {
				if(j % temp == 0) {
					nums[j] = 0;
				}
			}
		}
		
		int min = 10001;
		int sum = 0;
		
		for(int i=N; i>=M; i--) {
			if(nums[i] != 0) {
				min = nums[i];
				sum += nums[i];
			}
		}
		if(min  == 10001) {
			System.out.println(-1);
			return;
		}
		System.out.println(sum);
		System.out.println(min);
	}
	

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

댓글