Skip to content

Instantly share code, notes, and snippets.

@shelajev
Created November 13, 2020 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelajev/34af71f2eecceca927fb83cdd0395000 to your computer and use it in GitHub Desktop.
Save shelajev/34af71f2eecceca927fb83cdd0395000 to your computer and use it in GitHub Desktop.
package primes;
import javax.inject.Singleton;
import java.util.stream.*;
import java.util.*;
@Singleton
public class PrimesComputer {
private Random r = new Random(41);
public List<Long> random(int upperbound) {
int to = 2 + r.nextInt(upperbound - 2);
int from = 1 + r.nextInt(to - 1);
return primeSequence(from, to);
}
public static List<Long> primeSequence(long min, long max) {
return LongStream.range(min, max)
.filter(PrimesComputer::isPrime)
.boxed()
.collect(Collectors.toList());
}
public static boolean isPrime(long n) {
return LongStream.rangeClosed(2, (long) Math.sqrt(n))
.allMatch(i -> n % i != 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment