Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

The prime number sieve is mentioned in another answermentioned in another answer, but it's a little harder to use if you want to find a number of primes, rather than all the primes up to a number

The prime number sieve is mentioned in another answer, but it's a little harder to use if you want to find a number of primes, rather than all the primes up to a number

The prime number sieve is mentioned in another answer, but it's a little harder to use if you want to find a number of primes, rather than all the primes up to a number

edited body
Source Link

Now create an infinite stream using IntStream.iterate(2, i -> i + 1). All you need to do, ifis filter the stream so that each number generated is not divisible by each of the primes already generated:

System.out.printlnprintf("Total calculation time: %s ms"ms%n", (timeNow - l));

Now create an infinite stream using IntStream.iterate(2, i -> i + 1). All you need to do, if filter the stream so that each number generated is not divisible by each of the primes already generated:

System.out.println("Total calculation time: %s ms", (timeNow - l));

Now create an infinite stream using IntStream.iterate(2, i -> i + 1). All you need to do, is filter the stream so that each number generated is not divisible by each of the primes already generated:

System.out.printf("Total calculation time: %s ms%n", (timeNow - l));
added 16 characters in body
Source Link

The prime number sieve mentioned aboveis mentioned in another answer, but it's a little harder to use if you want to find a number of primes, rather than all the primes up to a number

You might also want to consider using a Console rather than a Scanner as you can prompt for input rather than have to println then read.

P.S. pre Java 8 primes would look like this:

Collection<Integer> primes(int numPrimes) {
    final List<Integer> primes = new ArrayList<>(numPrimes);
    int num = 2;
    while (primes.size() < numPrimes) {
        if (isPrime(num, primes))
            primes.add(num);
        num++;
    }
    return primes;
}


boolean isPrime(final int num, final List<Integer> primes) {
    for (int prime : primes)
        if (num % prime == 0)
            return false;
    return true;
}

The sieve mentioned above, but it's a little harder to use if you want to find a number of primes, rather than all the primes up to a number

You might also want to consider using a Console rather than a Scanner as you can prompt for input rather than have to println then read.

The prime number sieve is mentioned in another answer, but it's a little harder to use if you want to find a number of primes, rather than all the primes up to a number

You might also want to consider using a Console rather than a Scanner as you can prompt for input rather than have to println then read.

P.S. pre Java 8 primes would look like this:

Collection<Integer> primes(int numPrimes) {
    final List<Integer> primes = new ArrayList<>(numPrimes);
    int num = 2;
    while (primes.size() < numPrimes) {
        if (isPrime(num, primes))
            primes.add(num);
        num++;
    }
    return primes;
}


boolean isPrime(final int num, final List<Integer> primes) {
    for (int prime : primes)
        if (num % prime == 0)
            return false;
    return true;
}
added 1422 characters in body
Source Link
Loading
Source Link
Loading