Nth Prime
Prime number is an age old problem. I am trying to solve it via streams.
class PrimeCalculator {
public int nth(int nth) {
if (nth <= 0)
throw new IllegalArgumentException();
// Will skip the nth - 1
// 2 to n-1
return IntStream.iterate(2, i -> i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();
}
public static boolean isPrime(int x) {
// till half of x
return IntStream.rangeClosed(2, (int) (x/2) ).dropWhile(i -> x % i != 0).findFirst().isEmpty();
}
}
Explanation
public int nth(int nth) {
if (nth <= 0)
throw new IllegalArgumentException();
// Will skip the nth - 1
// 2 to n-1
return IntStream.iterate(2, i -> i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();
}
The method nth(int)
allows you to return the nth prime starting at 2, 3, 5....
- If the
nth
is negative or l.t.e 0 return an illegal argument exception. - Else, Iterate 2 onwards
- Filter all the elements that are prime.
- Skip all
n - 1
elements. - Find first and return it as int.
static <T> Stream<T> iterate(T seed,
UnaryOperator<T> f)
Stream<T> skip(long n)
`Skip()` Returns a stream consisting of the remaining elements of this stream after discarding the first
from java docsn
elements of the stream. If this stream contains fewer thann
elements then an empty stream will be returned.
Optional<T> findFirst()
An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result. A terminal operation is short-circuiting if, when presented with infinite input, it may terminate in finite time.
java docs
Helpers
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#iterate-T-java.util.function.UnaryOperator-
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#skip-long-
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps