Created
May 19, 2023 22:11
-
-
Save seujorgenochurras/dde669f6ad49c9ae7cc66f74ccce8498 to your computer and use it in GitHub Desktop.
prime generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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