카테고리 없음 [BOJ/백준 - 1929] 소수 구하기 - 문제 출처: https://www.acmicpc.net/problem/1929 1929번: 소수 구하기 첫째 줄에 자연수 M과 N이 빈 칸을 사이에 두고 주어진다. (1 ≤ M ≤ N ≤ 1,000,000) M이상 N이하의 소수가 하나 이상 있는 입력만 주어진다. www.acmicpc.net 해당 문제는 '에라토스테네스의 체'를 사용해 소수를 구하는 것이다. 그럼 그게 뭘까? 위키백과 ㄱㄱㄱ 에라토스테네스의 체 - 위키백과, 우리 모두의 백과사전 수학에서 에라토스테네스의 체는 소수를 찾는 방법이다. 고대 그리스 수학자 에라토스테네스가 발견하였다. 알고리즘[편집] 2부터 소수를 구하고자 하는 구간의 모든 수를 나열한다. 그림에서 ko.wikipedia.org 대충 gif만 보고 코드 짯음 This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters public class BOJ_1929 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int m = sc.nextInt(); int n = sc.nextInt(); int[] arr = new int[n+1]; for(int i=2; i<=n; i++){ if(arr[i] == 0){ // This is Prime Number if(i>=m){ sb.append(i).append("\n"); } }else{ continue; } int idx = 2; while(i*idx <= n){ arr[i*idx] = 1; idx++; } } System.out.print(sb); } } view raw BOJ_1929.java hosted with ❤ by GitHub 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기Time to lazy Contents 댓글 0 + 이전 댓글 더보기