Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.bobocode.fp;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.math.BigInteger.TWO;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

Expand All @@ -25,8 +28,8 @@ private PrimeNumbers() {
* @return an infinite int stream of prime numbers
*/
public static IntStream stream() {
return IntStream.iterate(2, i -> i + 1)
.filter(PrimeNumbers::isPrime);
return Stream.iterate(TWO, BigInteger::nextProbablePrime)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kastyan-kg that's a good option. 👍 However, this exercise is about Stream API, iteration, and ranges. So I would still encourage people to implement isPrime manually. What do you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough ;) just was thinking how to do this faster

.mapToInt(BigInteger::intValue);
}

/**
Expand Down Expand Up @@ -92,8 +95,4 @@ public static Map<Integer, List<Integer>> groupByAmountOfDigits(int n) {
.collect(groupingBy(x -> (int) (Math.log10(x) + 1)));
}

private static boolean isPrime(int n) {
return (n != 1) && IntStream.range(2, n)
.noneMatch(i -> n % i == 0);
}
}