Created
September 22, 2016 15:30
-
-
Save devetude/79c652f63eb21f40540f197b8b983f2d to your computer and use it in GitHub Desktop.
에라토네스의 체를 이용한 1부터 1000000까지 소수구하기 (Find Prime Number Between 1 and 1000000 By Using Sieve of Eratosthenes)
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
/** | |
* 에라토네스의 체를 이용한 1부터 1000000까지 소수구하기 (Find Prime Number Between 1 and 1000000 By Using Sieve of Eratosthenes) | |
* | |
* @author devetue | |
*/ | |
public class Main { | |
public static void main(String args[]) { | |
final int LIMIT = 1000000; | |
int[] sieve = new int[LIMIT + 1]; | |
sieve[0] = 1; | |
sieve[1] = 1; | |
for (int i = 2; i <= LIMIT; i++) { | |
if (sieve[i] == 1) { | |
continue; | |
} | |
int sqrtI = (int) Math.sqrt(i); | |
for (int j = 2; j <= sqrtI; j++) { | |
if (i % j == 0) { | |
int k = 1; | |
while (i * k <= LIMIT) { | |
sieve[i * k] = 1; | |
k++; | |
} | |
break; | |
} | |
} | |
} | |
for (int i = 1; i <= LIMIT; i++) { | |
if (sieve[i] == 0) { | |
System.out.println(i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment