Skip to content

Instantly share code, notes, and snippets.

@hagun
Created November 17, 2015 08:37
Show Gist options
  • Select an option

  • Save hagun/3dcb059da0018330ce19 to your computer and use it in GitHub Desktop.

Select an option

Save hagun/3dcb059da0018330ce19 to your computer and use it in GitHub Desktop.
에라토스테네스의 체
public void prime() {
long startAt = System.currentTimeMillis();
long max = 100000L;
SortedSet<Long> arr = new TreeSet<>();
for (long i = 2 ; i < max ; i++) {
arr.add(new Long(i));
}
long last = 0L;
do {
Long prime = arr.first();
System.out.println("prime : " + prime);
last = prime;
arr.remove(prime);
Long i = 2L;
Long notPrime = 0L;
do {
notPrime = prime * i;
arr.remove(notPrime);
i++;
} while(notPrime < max);
} while(arr.size() != 0);
long endAt = System.currentTimeMillis();
System.out.println(endAt - startAt);
System.out.println(last);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment