Skip to content

Instantly share code, notes, and snippets.

@seujorgenochurras
Created May 19, 2023 22:11
Show Gist options
  • Save seujorgenochurras/dde669f6ad49c9ae7cc66f74ccce8498 to your computer and use it in GitHub Desktop.
Save seujorgenochurras/dde669f6ad49c9ae7cc66f74ccce8498 to your computer and use it in GitHub Desktop.
prime generator
public class Main {
public static void main(String[] args){
long startedAt = System.nanoTime();
HashSet<Long> primes = new HashSet<>();
primes.add(2L);
for(long i = 1; i < 10000000; i+= 2) {
boolean isPrime = true;
for (int j = 3; j < Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.add(i);
}
}
long endedAt = System.nanoTime();
System.out.println(primes.size());
System.out.println("Took " + ((endedAt - startedAt) / 1000000 ));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment