Skip to content

Instantly share code, notes, and snippets.

@BrendenHJH
Created May 9, 2018 06:25
Show Gist options
  • Save BrendenHJH/0b8a93fde9f0a09ca76e20ab6bf88b6c to your computer and use it in GitHub Desktop.
Save BrendenHJH/0b8a93fde9f0a09ca76e20ab6bf88b6c to your computer and use it in GitHub Desktop.
에라토스테네스의 체(Sieve of Eratosthenes)
import java.util.ArrayList;
public class Main {
public static final int MAX = 25;
public static void main(String[] args) {
ArrayList<Integer> primeList = new ArrayList<Integer>();
boolean isPrime[] = new boolean[MAX + 1];
for(int i = 2; i <= MAX; i++) {
isPrime[i] = true;
}
for(int i = 2; i <= MAX; i++) {
if(!isPrime[i]) continue;
else primeList.add(i);
for(int j = i*2; j <= MAX; j += i) {
isPrime[j] = false;
}
}
for(int i = 0; i < primeList.size(); i++) {
System.out.println(primeList.get(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment