Created
February 22, 2019 21:33
-
-
Save etexier/17084ac75e5dcdc2f2764ca8b1122832 to your computer and use it in GitHub Desktop.
Prime numbers
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
/** | |
* Utility method for prime numbers. | |
**/ | |
public class Prime { | |
public static boolean isPrime(long n) { | |
return n > 1 && LongStream.rangeClosed(2, (long) Math.sqrt(n)).noneMatch(divisor -> n % divisor == 0); | |
} | |
public static long countPrimes(int max) { | |
return LongStream.range(1, max).filter(Prime::isPrime).count(); | |
} | |
public static long countPrimesInParallel(int max) { | |
return LongStream.range(1, max).filter(Prime::isPrime).count(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment