Related Articles

# Program to check for irreducibility using Eisenstein’s Irreducibility Criterion

• Difficulty Level : Expert
• Last Updated : 07 Aug, 2021

Given an array arr[] consisting of N integers such that each array element arr[i] represent coefficients of a polynomial expression starting with the highest degree(A[0].X(N – 1) + A[1].X(N – 2) + … + A[N – 2].X + A[N – 1]), the task is to check if the given equation is irreducible or not by Eisenstein’s Irreducibility Criterion or not. If found to be true, then print Yes. Otherwise, print No.

Examples:

Input: arr[] = {4, 7, 21, 28}
Output: Yes
Explanation:
The given array arr[] represents the polynomial 4x3 + 7x2 + 21x + 28.
The prime number 7 satisfies the conditions of Eisenstein’s Irreducibility conditions and thus, the polynomial is irreducible.

Input: arr[] = {1, 2, 1}
Output: No

Approach: Consider F(x) = anxn + an – 1xn – 1 + … + a0. The conditions that need to be satisfied to satisfy Eisenstein’s Irreducibility Criterion are as follows:

• There exists a prime number P such that:
• P does not divide an.
• P divides all other coefficients i.e., aN – 1, aN – 2, …, a0.
• P2 does not divide a0.

Follow the steps below to solve the problem:

• Initialize a variable, say M to -1, that stores the maximum value of A.
• Create a vector, say prime[] that contains all prime numbers less than and equal to A.
• Traverse through the primes array and for each current index i, do the following:
• Check if the current prime number primes[i] satisfies all 3 conditions i.e.,
• A[0] is not divisible by primes[i].
• All numbers from A[1] to A[N – 1] are all divisible by primes[i].
• A[N-1] is not divisible by primes[i].
• If the number satisfies all three conditions, the polynomial is irreducible, therefore, print Yes. Otherwise, print No.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to to implement the sieve``// of eratosthenes``vector<``int``> SieveOfEratosthenes(``int` `M)``{``    ``// Stores the prime numbers``    ``bool` `isPrime[M + 1];` `    ``// Initialize the prime numbers``    ``memset``(isPrime, ``true``,``           ``sizeof``(isPrime));` `    ``for` `(``int` `p = 2; p * p <= M; p++) {` `        ``// If isPrime[p] is not changed,``        ``// then it is a prime``        ``if` `(isPrime[p] == ``true``) {` `            ``// Update all multiples of``            ``// p as non-prime``            ``for` `(``int` `i = p * p;``                 ``i <= M; i += p) {``                ``isPrime[i] = ``false``;``            ``}``        ``}``    ``}` `    ``// Stores all prime numbers less``    ``// than M``    ``vector<``int``> prime;` `    ``for` `(``int` `i = 2; i <= M; i++) {` `        ``// If the i is the prime numbers``        ``if` `(isPrime[i]) {``            ``prime.push_back(i);``        ``}``    ``}` `    ``// Return array having the primes``    ``return` `prime;``}` `// Function to check whether the three``// conditions of Eisenstein's``// Irreducibility criterion for prime P``bool` `check(``int` `A[], ``int` `P, ``int` `N)``{``    ``// 1st condition``    ``if` `(A[0] % P == 0)``        ``return` `0;` `    ``// 2nd condition``    ``for` `(``int` `i = 1; i < N; i++)``        ``if` `(A[i] % P)``            ``return` `0;` `    ``// 3rd condition``    ``if` `(A[N - 1] % (P * P) == 0)``        ``return` `0;` `    ``return` `1;``}``// Function to check for Eisensteins``// Irreducubility Criterion``bool` `checkIrreducibilty(``int` `A[], ``int` `N)``{``    ``// Stores the largest element in A``    ``int` `M = -1;` `    ``// Find the maximum element in A``    ``for` `(``int` `i = 0; i < N; i++) {``        ``M = max(M, A[i]);``    ``}` `    ``// Stores all the prime numbers``    ``vector<``int``> primes``        ``= SieveOfEratosthenes(M + 1);` `    ``// Check if any prime``    ``// satisfies the conditions``    ``for` `(``int` `i = 0;``         ``i < primes.size(); i++) {` `        ``// Function Call to check``        ``// for the three conditions``        ``if` `(check(A, primes[i], N)) {``            ``return` `1;``        ``}``    ``}``    ``return` `0;``}` `// Driver Code``int` `main()``{``    ``int` `A[] = { 4, 7, 21, 28 };``    ``int` `N = ``sizeof``(A) / ``sizeof``(A[0]);``    ``cout << checkIrreducibilty(A, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to to implement the sieve``// of eratosthenes``static` `ArrayList SieveOfEratosthenes(``int` `M)``{``    ` `    ``// Stores the prime numbers``    ``boolean` `[]isPrime = ``new` `boolean``[M + ``1``];` `    ``// Initialize the prime numbers``    ``for``(``int` `i = ``0``; i < M + ``1``; i++)``        ``isPrime[i] = ``true``;` `    ``for``(``int` `p = ``2``; p * p <= M; p++)``    ``{``        ` `        ``// If isPrime[p] is not changed,``        ``// then it is a prime``        ``if` `(isPrime[p] == ``true``)``        ``{``            ` `            ``// Update all multiples of``            ``// p as non-prime``            ``for``(``int` `i = p * p; i <= M; i += p)``            ``{``                ``isPrime[i] = ``false``;``            ``}``        ``}``    ``}` `    ``// Stores all prime numbers less``    ``// than M``    ``ArrayList prime = ``new` `ArrayList();` `    ``for``(``int` `i = ``2``; i <= M; i++)``    ``{``        ` `        ``// If the i is the prime numbers``        ``if` `(isPrime[i])``        ``{``            ``prime.add(i);``        ``}``    ``}` `    ``// Return array having the primes``    ``return` `prime;``}` `// Function to check whether the three``// conditions of Eisenstein's``// Irreducibility criterion for prime P``static` `int` `check(``int` `[]A, ``int` `P, ``int` `N)``{``    ` `    ``// 1st condition``    ``if` `(A[``0``] % P == ``0``)``        ``return` `0``;` `    ``// 2nd condition``    ``for``(``int` `i = ``1``; i < N; i++)``        ``if` `(A[i] % P != ``0``)``            ``return` `0``;` `    ``// 3rd condition``    ``if` `(A[N - ``1``] % (P * P) == ``0``)``        ``return` `0``;` `    ``return` `1``;``}` `// Function to check for Eisensteins``// Irreducubility Criterion``static` `int` `checkIrreducibilty(``int` `[]A, ``int` `N)``{``    ` `    ``// Stores the largest element in A``    ``int` `M = -``1``;` `    ``// Find the maximum element in A``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ``M = Math.max(M, A[i]);``    ``}` `    ``// Stores all the prime numbers``    ``ArrayList primes = SieveOfEratosthenes(M + ``1``);` `    ``// Check if any prime``    ``// satisfies the conditions``    ``for``(``int` `i = ``0``; i < primes.size(); i++)``    ``{``        ` `        ``// Function Call to check``        ``// for the three conditions``        ``if` `(check(A, primes.get(i), N) == ``1``)``        ``{``            ``return` `1``;``        ``}``    ``}``    ``return` `0``;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `[]A = { ``4``, ``7``, ``21``, ``28` `};``    ``int` `N = A.length;``    ` `    ``System.out.print(checkIrreducibilty(A, N));``}``}` `// This code is contributed by avijitmondal1998`

## Python3

 `# Python3 program for the above approach` `# Function to to implement the sieve``# of eratosthenes``def` `SieveOfEratosthenes(M):``  ` `    ``# Stores the prime numbers``    ``isPrime ``=` `[``True``]``*``(M ``+` `1``)` `    ``for` `p ``in` `range``(``2``, M ``+` `1``):``        ``if` `p ``*` `p > M:``            ``break` `        ``# If isPrime[p] is not changed,``        ``# then it is a prime``        ``if` `(isPrime[p] ``=``=` `True``):` `            ``# Update all multiples of``            ``# p as non-prime``            ``for` `i ``in` `range``(p ``*` `p, M ``+` `1``, p):``                ``isPrime[i] ``=` `False` `    ``# Stores all prime numbers less``    ``# than M``    ``prime ``=` `[]` `    ``for` `i ``in` `range``(``2``, M ``+` `1``):``      ` `        ``# If the i is the prime numbers``        ``if` `(isPrime[i]):``            ``prime.append(i)` `    ``# Return array having the primes``    ``return` `prime` `# Function to check whether the three``# conditions of Eisenstein's``# Irreducibility criterion for prime P``def` `check(A, P, N):``    ``# 1st condition``    ``if` `(A[``0``] ``%` `P ``=``=` `0``):``        ``return` `0` `    ``# 2nd condition``    ``for` `i ``in` `range``(``1``,N):``        ``if` `(A[i] ``%` `P):``            ``return` `0` `    ``# 3rd condition``    ``if` `(A[N ``-` `1``] ``%` `(P ``*` `P) ``=``=` `0``):``        ``return` `0` `    ``return` `1``# Function to check for Eisensteins``# Irreducubility Criterion``def` `checkIrreducibilty(A, N):``    ``# Stores the largest element in A``    ``M ``=` `-``1` `    ``# Find the maximum element in A``    ``for` `i ``in` `range``(N):``        ``M ``=` `max``(M, A[i])` `    ``# Stores all the prime numbers``    ``primes ``=` `SieveOfEratosthenes(M ``+` `1``)` `    ``# Check if any prime``    ``# satisfies the conditions``    ``for` `i ``in` `range``(``len``(primes)):` `        ``# Function Call to check``        ``# for the three conditions``        ``if` `(check(A, primes[i], N)):``            ``return` `1``    ``return` `0` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``A ``=` `[``4``, ``7``, ``21``, ``28``]``    ``N ``=` `len``(A)``    ``print` `(checkIrreducibilty(A, N))` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{`` ` `// Function to to implement the sieve``// of eratosthenes``static` `List<``int``> SieveOfEratosthenes(``int` `M)``{``    ` `    ``// Stores the prime numbers``    ``bool` `[]isPrime = ``new` `bool``[M + 1];` `    ``// Initialize the prime numbers``    ``for``(``int` `i = 0; i < M + 1; i++)``        ``isPrime[i] = ``true``;` `    ``for``(``int` `p = 2; p * p <= M; p++)``    ``{``        ` `        ``// If isPrime[p] is not changed,``        ``// then it is a prime``        ``if` `(isPrime[p] == ``true``)``        ``{``            ` `            ``// Update all multiples of``            ``// p as non-prime``            ``for``(``int` `i = p * p; i <= M; i += p)``            ``{``                ``isPrime[i] = ``false``;``            ``}``        ``}``    ``}` `    ``// Stores all prime numbers less``    ``// than M``    ``List<``int``> prime = ``new` `List<``int``>();` `    ``for``(``int` `i = 2; i <= M; i++)``    ``{``        ` `        ``// If the i is the prime numbers``        ``if` `(isPrime[i])``        ``{``            ``prime.Add(i);``        ``}``    ``}` `    ``// Return array having the primes``    ``return` `prime;``}` `// Function to check whether the three``// conditions of Eisenstein's``// Irreducibility criterion for prime P``static` `int` `check(``int` `[]A, ``int` `P, ``int` `N)``{``    ` `    ``// 1st condition``    ``if` `(A[0] % P == 0)``        ``return` `0;` `    ``// 2nd condition``    ``for``(``int` `i = 1; i < N; i++)``        ``if` `(A[i] % P !=0)``            ``return` `0;` `    ``// 3rd condition``    ``if` `(A[N - 1] % (P * P) == 0)``        ``return` `0;` `    ``return` `1;``}` `// Function to check for Eisensteins``// Irreducubility Criterion``static` `int` `checkIrreducibilty(``int` `[]A, ``int` `N)``{``    ` `    ``// Stores the largest element in A``    ``int` `M = -1;` `    ``// Find the maximum element in A``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ``M = Math.Max(M, A[i]);``    ``}` `    ``// Stores all the prime numbers``    ``List<``int``> primes = SieveOfEratosthenes(M + 1);` `    ``// Check if any prime``    ``// satisfies the conditions``    ``for``(``int` `i = 0; i < primes.Count; i++)``    ``{``        ` `        ``// Function Call to check``        ``// for the three conditions``        ``if` `(check(A, primes[i], N) == 1)``        ``{``            ``return` `1;``        ``}``    ``}``    ``return` `0;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `[]A = { 4, 7, 21, 28 };``    ``int` `N = A.Length;``    ` `    ``Console.Write(checkIrreducibilty(A, N));``}``}` `// This code is contributed by SURENDRA_GANGWAR`

## Javascript

 ``
Output:
`1`

Time Complexity: O(M + N*P), where M is the maximum element in the array and P is the number of primes less than N
Auxiliary Space: O(P)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up