使用 Eisenstein 的不可约性准则检查不可约性的程序
给定一个由N个整数组成的数组arr[] ,使得每个数组元素arr[i]表示从最高次数开始的多项式表达式的系数 ( A[0].X (N – 1) + A[1].X ( N – 2) + … + A[N – 2].X + A[N – 1]) ,任务是根据爱森斯坦的不可约准则检查给定方程是否不可约。如果发现是真的,则打印Yes 。否则,打印No 。
例子:
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
方法:考虑F(x) = a n x n + a n – 1 x n – 1 + … + a 0 。满足 E isenstein 不可约判据需要满足的条件如下:
- 存在一个素数P使得:
- P不整除 a n 。
- P除以所有其他系数,即 a N – 1、 a N – 2 、...、a 0 。
- P 2不整除一个0 。
请按照以下步骤解决问题:
- 初始化一个变量,比如M到-1,它存储A的最大值。
- 创建一个向量,比如说prime[] ,它包含所有小于和等于A 的素数。
- 遍历素数数组并为每个当前索引i执行以下操作:
- 检查当前素数 primes[i]是否满足所有 3 个条件,即,
- A[0]不能被primes[i]整除。
- 从A[1]到A[N – 1]的所有数字都可以被primes[i]整除。
- A [N-1]不能被primes[i]整除。
- 如果数字满足所有三个条件,则多项式是不可约的,因此,打印Yes 。否则,打印No 。
- 检查当前素数 primes[i]是否满足所有 3 个条件,即,
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to to implement the sieve
// of eratosthenes
vector 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 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 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 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 prime = new List();
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 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
输出:
1
时间复杂度: O(M + N*P),其中 M 是数组中的最大元素, P 是小于 N 的素数数
辅助空间: O(P)