Skip to content

Instantly share code, notes, and snippets.

@dineshrajpurohit
Created February 20, 2015 03:46
Show Gist options
  • Save dineshrajpurohit/d643ef35a66737d769d6 to your computer and use it in GitHub Desktop.
Save dineshrajpurohit/d643ef35a66737d769d6 to your computer and use it in GitHub Desktop.
// Sieve of erosthenes
public static void primes(int n){
Boolean[] prime = new Boolean[n+1];
for(int i=2;i<=n;i++){
prime[i] = true;
}
for(int div=2;div*div<=n;div++){
if(prime[div] == true){
for(int i=2*div;i<=n;i=i+div){
prime[i] = false;
}
}
}
for(int i=2;i<=n;i++){
if(prime[i]){
System.out.print(i+ " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment