Javatpoint Logo
Javatpoint Logo

Euclid-Mullin Sequence in Java

The Euclid-Mullin Sequence is a mathematical sequence of prime numbers characterized by a recursive definition. In more technical terms, this sequence starts with the number 2 as its first term. Subsequent terms are generated by finding prime numbers that satisfy a specific condition. In this sequence, every new term is the smallest prime number. When you divide it by the previous term and add 1, the result is another prime number.

Here's a more technical overview of the process:

  1. The first term in the sequence is always 2.
  2. To identify the next term, look for the smallest prime number (larger than the previous term) that, when divided by the previous term and increment by 1, resulting in a prime number.
  3. The process continues iteratively to generate additional terms.

Example 1:

Input: N = 20


2 3 7 43 13 53 5 6221671 38709183810571 139 2801 11 17 5471 52662739 233 2207 1746860020068409 761 5403705334863907824909

Example 2:

Input: N = 5


2 3 7 43 13

Example 3:

Input: N = 8


2 3 7 43 13 53 5 6221671

Approach: Greedy Algorithm

In the Euclid-Mullin sequence, the greedy algorithm works by repeatedly finding the smallest prime factor of (1 + product), where the product is the product of the previous terms in the sequence. It is the next term in the sequence. The algorithm then updates the product variable with the next term. The process is continued until the desired number of terms are created.


  1. Create a method called 'findSmallestPrimeFactor' that accepts a BigInteger integer 'num' as input.
  2. Initialize a BigInteger variable `i' to 2.
  3. Use a while loop to find the smallest prime factor of `num`:
    1. Determine whether 'i * i' is less than or equal to 'num'.
    2. Return 'i' as the smallest prime factor if 'num' is divisible by 'i' (i.e., 'num' modulo 'i' is zero).
    3. Increment ` i' by one.
  4. If no smaller factor is found, return `num` itself as the smallest prime factor.
  5. Create a method named `generateEuclidMullinSequence` that takes a BigInteger `N` as input.
  6. Initialize a BigInteger variable `product` to 1 to keep track of the product of previous terms.
  7. Initialize a BigInteger variable `termCounter` to 0 to count the terms generated.
  8. Use a while loop to generate and print the first N terms of the sequence:
    1. Calculate the current term as the smallest prime factor of `(1 + product)` using the `findSmallestPrimeFactor` method.
    2. Print the current term.
    3. If it's different from the last term, print a space.
    4. Update the product by multiplying it with the current term.
    5. Increment the term counter by one.
  9. In the `main` Method, set the number of terms to generate (in this case, 14) using a BigInteger variable called`numberOfTerms`
  10. Call the `generateEuclidMullinSequence` method with `numberOfTerms` to produce and display the first 14 terms of the Euclid-Mullin Sequence.




Euclid-Mullin Sequence: 2 3 7 4313535 6221671 38709183810571 139 280111 17 5471

Time Complexity: The code has a time complexity of approximately O(N * sqrt(num)), where N is the number of terms to generate in the Euclid-Mullin Sequence.

Auxiliary Space: The code has an auxiliary space complexity of O(1), meaning it uses a constant amount of memory that doesn't depend on the input size.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA