SWEA 3131 100만 이하의 모든 소수

1 개요[ | ]

SWEA 3131 100만 이하의 모든 소수
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 3-5

2 C++[ | ]

3 Java[ | ]

public class Solution {
	public static void main(String[] args) {
		int MAX = 1000000;
		boolean[] a = new boolean[MAX+1];
		a[0] = a[1] = true;
		for(int i=2; i<=Math.sqrt(MAX); i++) {
			if( a[i] ) continue;
			for(int j=i*2; j<=MAX; j+=i) a[j] = true;
		}
		StringBuilder sb = new StringBuilder();
		for(int i=2; i<=MAX; i++) {
			if( !a[i] ) {
				sb.append(i);
				sb.append(" ");
			}
		}
		System.out.println( sb );
	}
}

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}