A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 6 is composite because it is the product of two numbers (2 × 3) that are both smaller than 6. The property of being prime is called primality. A simple method of checking the primality of a given number n, called trial division, tests whether n is a multiple of any integer between 2 and square root of n.

In this post, I will demonstrate how to check primality of a number and then generate a sequence of prime numbers using Java Stream API based on two different criteria.