Java - Creating an infinite stream of prime numbers

Introduction

PrimeUtil class contains two methods.

The next() instance method returns the next prime number after the last found prime number.

The next(long after) static method returns the prime number after the specified number.

The isPrime() static method checks if a number is a prime number.

The following code creates an infinite stream of prime numbers and prints the first five prime numbers on the standard output:

Demo

import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    Stream.iterate(2L, PrimeUtil::next).limit(5).forEach(System.out::println);

  }//from   www . ja va2  s  .  c om
}

class PrimeUtil {
  private long lastPrime = 0L;

  // Computes the prime number after the last generated prime
  public long next() {
    lastPrime = next(lastPrime);
    return lastPrime;
  }

  // Computes the prime number after the specified number
  public static long next(long after) {
    long counter = after;

    // Keep looping until you find the next prime number
    while (!isPrime(++counter))
      ;

    return counter;
  }

  // Checks if the specified number is a prime number
  public static boolean isPrime(long number) {
    // <= 1 is not a prime number
    if (number <= 1) {
      return false;
    }

    // 2 is a prime number
    if (number == 2) {
      return true;
    }

    // Even numbers > 2 are not prime numbers
    if (number % 2 == 0) {
      return false;
    }

    long maxDivisor = (long) Math.sqrt(number);
    for (int counter = 3; counter <= maxDivisor; counter += 2) {
      if (number % counter == 0) {
        return false;
      }
    }

    return true;
  }
}

Result

Related Topic