# Euclid–Mullin Sequence

Given an integer N, the task is to print the first N elements of the Euclid-Mullin Sequence. The Euclid-Mullin sequence is a sequence of prime numbers where each element is the least prime factor of one plus the product of all earlier elements. The sequence is named after the ancient Greek mathematician Euclid. Examples:

Input: N = 14 Output: 2 3 7 43 13 53 5 6221671 38709183810571 139 2801 11 17 5471

Approach: The Euclid–Mullin sequence is a sequence of prime numbers where the nth number of sequence is: So, we will run a loop from 1 to N and take a variable product which is initially to 1 and will contain the product of all previous elements. We will then find the smallest prime factor of (1 + product) in O(sqrt(n)) time and print the number. Note that the code fails to print numbers after the 14th element as the product becomes too large and finding its smallest prime factor takes a lot of time. Below is the implementation of the above approach:

## C++

 // C++ implementation of the approach #include using namespace std; // Function to return the smallest prime factor of n unsigned long long smallestPrimeFactor(unsigned long long n){    // Initialize i = 2     unsigned long long i = 2;     // While i <= sqrt(n)     while ((i * i) <= n)     {        // If n is divisible by i         if (n % i == 0)             return i;         // Increment i         i += 1;    }    return n;}  // Function to print the first n // terms of the required sequence void solve(unsigned long long n) {    // To store the product of the previous terms     unsigned long long product = 1;     // Traverse the prime numbers     unsigned long long i = 0;    while (i < n)     {        // Current term will be smallest prime         // factor of (1 + product of all previous terms)        unsigned long long num = smallestPrimeFactor(product + 1);         // Print the current term         cout << num << " ";          // Update the product         product = product * num;        i += 1;    }} // Driver code int main(){    // Find the first 14 terms of the sequence     unsigned long long b = 14;    solve(b);} // This code is contributed by phasing17

## Java

 // Java implementation of the approachimport java.math.BigInteger;class GFG {     // Function to return the smallest prime factor of n    static BigInteger smallestPrimeFactor(BigInteger n)    {         // Initialize i = 2        BigInteger i = BigInteger.valueOf(2);         // While i <= sqrt(n)        while ((i.multiply(i)).compareTo(n) <= 0) {             // If n is divisible by i            if (n.mod(i).compareTo(BigInteger.ZERO) == 0)                return i;             // Increment i            i = i.add(BigInteger.ONE);        }        return n;    }     // Function to print the first n    // terms of the required sequence    static void solve(BigInteger n)    {        // To store the product of the previous terms        BigInteger product = BigInteger.ONE;         // Traverse the prime numbers        BigInteger i = BigInteger.ZERO;        while (i.compareTo(n) < 0) {             // Current term will be smallest prime            // factor of (1 + product of all previous terms)            BigInteger num = smallestPrimeFactor(product.add(BigInteger.ONE));             // Print the current term            System.out.print(num + " ");             // Update the product            product = product.multiply(num);            i = i.add(BigInteger.ONE);        }    }     // Driver code    public static void main(String[] args)    {         // Find the first 14 terms of the sequence        BigInteger b = BigInteger.valueOf(14);        solve(b);    }}

## C#

 // C# implementation of the approach using System;using System.Collections.Generic; class GFG{  // Function to return the smallest prime factor of n   static ulong smallestPrimeFactor(ulong n)  {    // Initialize i = 2     ulong i = 2;     // While i <= sqrt(n)     while ((i * i) <= n)     {      // If n is divisible by i       if (n % i == 0)         return i;       // Increment i       i += 1;    }    return n;  }    // Function to print the first n   // terms of the required sequence   static void solve(ulong n)   {    // To store the product of the previous terms     ulong product = 1;     // Traverse the prime numbers     ulong i = 0;    while (i < n)     {      // Current term will be smallest prime       // factor of (1 + product of all previous terms)      ulong num = smallestPrimeFactor(product + 1);       // Print the current term       Console.Write(num + " ");        // Update the product       product = product * num;      i += 1;    }  }   // Driver code   public static void Main(string[] args)  {    // Find the first 14 terms of the sequence     ulong b = 14;    solve(b);  }} // This code is contributed by phasing17

## Python3

 # Python3 implementation of the approach  # Function to return the smallest prime factor of n def smallestPrimeFactor(n):      # Initialize i = 2     i = 2     # While i <= sqrt(n)     while (i * i) <= n :          # If n is divisible by i         if n % i == 0:             return i          # Increment i         i += 1    return n  # Function to print the first n # terms of the required sequence def solve(n):      # To store the product of the previous terms     product = 1     # Traverse the prime numbers     i = 0    while i < n:          # Current term will be smallest prime         # factor of (1 + product of all previous terms)        num = smallestPrimeFactor(product + 1)          # Print the current term         print(num, end=' ')          # Update the product         product = product * num         i += 1 # Driver code # Find the first 14 terms of the sequence b = 14solve(b) # This code is contributed by divyamohan123

## Javascript

 // JavaScript implementation of the approach  // Function to return the smallest prime factor of n function smallestPrimeFactor(n){    // Initialize i = 2     let i = 2     // While i <= sqrt(n)     while ((i * i) <= n)     {        // If n is divisible by i         if (n % i == 0)             return i          // Increment i         i += 1    }    return n}  // Function to print the first n // terms of the required sequence function solve(n) {    // To store the product of the previous terms     let product = 1     // Traverse the prime numbers     let i = 0    while (i < n)     {        // Current term will be smallest prime         // factor of (1 + product of all previous terms)        let num = smallestPrimeFactor(product + 1)          // Print the current term         process.stdout.write(num + " ")          // Update the product         product = product * num         i += 1    }}  // Driver code // Find the first 14 terms of the sequence let b = 14solve(b)  // This code is contributed by phasing17

Output:
2 3 7 43 13 53 5 6221671 38709183810571 139 2801 11 17 5471

Time Complexity: O(n*sqrt(n))

Auxiliary Space: O(1)

Previous
Next