Check whether the given number is Euclid Number or not

Given a positive integer n, the task is to check if it is Euclid Number or not. Print ‘YES’ if the given number is Euclid Number otherwise print ‘NO’.

Euclid number : In Mathematics, Euclid numbers are integers of the form – E_{n} = p_{n}\# + 1
where p_{n}\# is product of first n prime numbers.

The first few Euclid numbers are-

3, 7, 31, 211, 2311, 30031, 510511, 9699691, ..........

Example:

Input: N = 31
Output: YES
31 can be expressed in the form of 
pn# + 1 as p3# + 1
(First 3 prime numbers are 2, 3, 5 and their product is 30 )

Input: N = 43
Output: NO
43 cannot be expressed in the form of pn# + 1

Naive Approach:

  1. Generate all prime number in the range using Sieve of Eratosthenes.
  2. Then starting from first prime (i.e 2 ) start multiplying next prime numberand keep checking if product + 1 = n .
  3. If the product + 1 = n then, n is Euclid number. Otherwise not.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check Euclid Number
  
#include <bits/stdc++.h>
using namespace std;
  
#define MAX 10000
  
vector<int> arr;
  
// Function to generate prime numbers
void SieveOfEratosthenes()
{
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    bool prime[MAX];
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p * p < MAX; p++) {
        // If prime[p] is not changed, then it is a prime
  
        if (prime[p] == true) {
  
            // Update all multiples of p
            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }
  
    // store all prime numbers
    // to vector 'arr'
    for (int p = 2; p < MAX; p++)
        if (prime[p])
            arr.push_back(p);
}
  
// Function to check the number for Euclid Number
bool isEuclid(long n)
{
  
    long long product = 1;
    int i = 0;
  
    while (product < n) {
  
        // Multiply next prime number
        // and check if product + 1 = n
        // holds or not
        product = product * arr[i];
  
        if (product + 1 == n)
            return true;
  
        i++;
    }
  
    return false;
}
  
// Driver code
int main()
{
  
    // Get the prime numbers
    SieveOfEratosthenes();
  
    // Get n
    long n = 31;
  
    // Check if n is Euclid Number
    if (isEuclid(n))
        cout << "YES\n";
    else
        cout << "NO\n";
  
    // Get n
    n = 42;
  
    // Check if n is Euclid Number
    if (isEuclid(n))
        cout << "YES\n";
    else
        cout << "NO\n";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check Euclid Number
  
import java.util.*;
  
class GFG {
  
    static final int MAX = 10000;
    static Vector<Integer> arr = new Vector<Integer>();
  
    // Function to get the prime numbers
    static void SieveOfEratosthenes()
    {
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        boolean[] prime = new boolean[MAX];
  
        for (int i = 0; i < MAX; i++)
            prime[i] = true;
  
        for (int p = 2; p * p < MAX; p++) {
  
            // If prime[p] is not changed, then it is a prime
            if (prime[p] == true) {
  
                // Update all multiples of p
                for (int i = p * 2; i < MAX; i += p)
                    prime[i] = false;
            }
        }
  
        // store all prime numbers
        // to vector 'arr'
        for (int p = 2; p < MAX; p++)
            if (prime[p])
                arr.add(p);
    }
  
    // Function to check the number for Euclid Number
    static boolean isEuclid(long n)
    {
  
        long product = 1;
        int i = 0;
        while (product < n) {
  
            // Multiply next prime number
            // and check if product + 1 = n
            // holds or not
            product = product * arr.get(i);
  
            if (product + 1 == n)
                return true;
  
            i++;
        }
  
        return false;
    }
    public static void main(String[] args)
    {
  
        // Get the prime numbers
        SieveOfEratosthenes();
  
        // Get n
        long n = 31;
  
        // Check if n is Euclid Number
        if (isEuclid(n))
            System.out.println("YES");
        else
            System.out.println("NO");
  
        // Get n
        n = 42;
  
        // Check if n is Euclid Number
        if (isEuclid(n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to check 
# Euclid Number
MAX = 10000
  
arr = []
  
# Function to generate prime numbers
def SieveOfEratosthenes():
  
    # Create a boolean array "prime[0..n]" 
    # and initialize all entries it as 
    # true. A value in prime[i] will
    # finally be false if i is Not a
    # prime, else true.
    prime = [True] * MAX
  
    p = 2
    while p * p < MAX :
          
        # If prime[p] is not changed, 
        # then it is a prime
        if (prime[p] == True):
  
            # Update all multiples of p
            for i in range(p * 2, MAX, p):
                prime[i] = False
                  
        p += 1
  
    # store all prime numbers
    # to vector 'arr'
    for p in range(2, MAX):
        if (prime[p]):
            arr.append(p)
  
# Function to check the number
# for Euclid Number
def isEuclid(n):
  
    product = 1
    i = 0
  
    while (product < n) :
  
        # Multiply next prime number
        # and check if product + 1 = n
        # holds or not
        product = product * arr[i]
  
        if (product + 1 == n):
            return True
  
        i += 1
  
    return False
  
# Driver code
if __name__ == "__main__":
  
    # Get the prime numbers
    SieveOfEratosthenes()
  
    # Get n
    n = 31
  
    # Check if n is Euclid Number
    if (isEuclid(n)):
        print("YES")
    else:
        print("NO")
  
    # Get n
    n = 42
  
    # Check if n is Euclid Number
    if (isEuclid(n)):
        print("YES")
    else:
        print("NO")
  
# This code is contributed 
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check Euclid Number
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    static readonly int MAX = 10000;
    static List<int> arr = new List<int>();
  
    // Function to get the prime numbers
    static void SieveOfEratosthenes()
    {
        // Create a boolean array 
        // "prime[0..n]" and initialize
        // all entries it as true. 
        // A value in prime[i] will
        // finally be false if i is
        // Not a prime, else true.
        bool[] prime = new bool[MAX];
  
        for (int i = 0; i < MAX; i++)
            prime[i] = true;
  
        for (int p = 2; p * p < MAX; p++)
        {
  
            // If prime[p] is not changed,
            // then it is a prime
            if (prime[p] == true
            {
  
                // Update all multiples of p
                for (int i = p * 2; i < MAX; i += p)
                    prime[i] = false;
            }
        }
  
        // store all prime numbers
        // to vector 'arr'
        for (int p = 2; p < MAX; p++)
            if (prime[p])
                arr.Add(p);
    }
  
    // Function to check the number for Euclid Number
    static bool isEuclid(long n)
    {
  
        long product = 1;
        int i = 0;
        while (product < n) 
        {
  
            // Multiply next prime number
            // and check if product + 1 = n
            // holds or not
            product = product * arr[i];
  
            if (product + 1 == n)
                return true;
  
            i++;
        }
  
        return false;
    }
      
    // Driver code
    public static void Main(String[] args)
    {
  
        // Get the prime numbers
        SieveOfEratosthenes();
  
        // Get n
        long n = 31;
  
        // Check if n is Euclid Number
        if (isEuclid(n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
  
        // Get n
        n = 42;
  
        // Check if n is Euclid Number
        if (isEuclid(n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right