Skip to content

Instantly share code, notes, and snippets.

@etexier
Created February 22, 2019 21:33
Show Gist options
  • Save etexier/17084ac75e5dcdc2f2764ca8b1122832 to your computer and use it in GitHub Desktop.
Save etexier/17084ac75e5dcdc2f2764ca8b1122832 to your computer and use it in GitHub Desktop.
Prime numbers
/**
* 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