Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Created October 19, 2014 22:12
Show Gist options
  • Save MLLeKander/0a3ab76d526109b95c7c to your computer and use it in GitHub Desktop.
Save MLLeKander/0a3ab76d526109b95c7c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes Implementation
class Sieve {
public static void main(String[] args) {
int MAX_VALUE = 10000;
boolean[] isComposite = new boolean[MAX_VALUE];
isComposite[1] = isComposite[0] = true;
for (int i = 2; i*i < MAX_VALUE; i++) {
if (!isComposite[i]) {
for (int o = i*2; o < MAX_VALUE; o += i) {
isComposite[o] = true;
}
}
}
for (int i = 0; i < MAX_VALUE; i++) {
System.out.println(i+": "+!isComposite[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment