📌  相关文章
📜  不与数组的任何元素互素的最小数字

📅  最后修改于: 2021-04-18 02:29:43             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到与给定数组的任何元素都不互素的最小数字。

例子:

方法:这个想法是基于观察到的,要求的数字(例如X)不应与任何数组元素arr [i]互素。必须存在一些共同的因素,d≥2对于每个阵列元素ARR [I],其将两个ARR [i]X.可能的最小d是素数。因此,我们的想法是考虑素数集,以使它们的乘积不与所有数组元素都互素,并找到可能的最小数。请按照以下步骤解决问题:

  • 将变量ans初始化为INT_MAX,以存储所需的结果。
  • 初始化一个数组primes []以存储素数。
  • 生成此数组的所有子集,并为每个子集将其乘积存储在变量(例如P)中。检查其是否与任何数组元素互素。如果发现为true,则更新ans的值。
  • 否则,请移至下一个子集。
  • 完成上述步骤后,输出ans的值作为结果。  

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
#define ll long long int
#define MAX 50
 
// Function check if a
// number is prime or not
bool isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // Check if n is divisible by 2 or 3
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    // Check for every 6th number. The above
    // checking allows to skip middle 5 numbers
    for (ll i = 5; i * i <= n; i = i + 6)
 
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Function to store primes in an array
void findPrime(vector& primes)
{
    for (ll i = 2; i <= MAX; i++) {
        if (isPrime(i))
            primes.push_back(i);
    }
}
 
// Function to calculate
// GCD of two numbers
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to find the smallest
// number which is not coprime with
// any element of the array arr[]
void findMinimumNumber(ll arr[], ll N)
{
    // Store the prime numbers
    vector primes;
 
    // Function call to fill
    // the prime numbers
    findPrime(primes);
 
    // Stores the answer
    ll ans = INT_MAX;
 
    ll n = primes.size();
 
    // Generate all non-empty
    // subsets of the primes[] array
    for (ll i = 1; i < (1 << n); i++) {
 
        // Stores product of the primes
        ll temp = 1;
        for (ll j = 0; j < n; j++) {
            if (i & (1 << j)) {
                temp *= primes[j];
            }
        }
 
        // Checks if temp is coprime
        // with the array or not
        bool check = true;
 
        // Check if the product temp is
        // not coprime with the whole array
        for (ll k = 0; k < N; k++) {
            if (gcd(temp, arr[k]) == 1) {
                check = false;
                break;
            }
        }
 
        // If the product is not
        // co-prime with the array
        if (check)
            ans = min(ans, temp);
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given array
    ll arr[] = { 3, 4, 6, 7, 8, 9, 10 };
 
    // Stores the size of the array
    ll N = sizeof(arr) / sizeof(arr[0]);
 
    findMinimumNumber(arr, N);
 
    return 0;
}


Python3
# Python 3 program for the above approach
MAX = 50
 
import sys
from math import sqrt,gcd
 
# Function check if a
# number is prime or not
def isPrime(n):
   
    # Corner cases
    if (n <= 1):
        return False
    if (n <= 3):
        return True
 
    # Check if n is divisible by 2 or 3
    if (n % 2 == 0 or n % 3 == 0):
        return False
 
    # Check for every 6th number. The above
    # checking allows to skip middle 5 numbers
    for i in range(5,int(sqrt(n))+1,6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
 
    return True
 
# Function to store primes in an array
def findPrime(primes):
    global MAX
    for i in range(2, MAX + 1, 1):
        if(isPrime(i)):
            primes.append(i)
 
# Function to find the smallest
# number which is not coprime with
# any element of the array arr[]
def findMinimumNumber(arr, N):
   
    # Store the prime numbers
    primes = []
 
    # Function call to fill
    # the prime numbers
    findPrime(primes)
 
    # Stores the answer
    ans = sys.maxsize
    n = len(primes)
 
    # Generate all non-empty
    # subsets of the primes[] array
    for i in range(1, (1 << n), 1):
       
        # Stores product of the primes
        temp = 1
        for j in range(n):
            if (i & (1 << j)):
                temp *= primes[j]
 
        # Checks if temp is coprime
        # with the array or not
        check = True
 
        # Check if the product temp is
        # not coprime with the whole array
        for k in range(N):
            if (gcd(temp, arr[k]) == 1):
                check = False
                break
 
        # If the product is not
        # co-prime with the array
        if (check):
            ans = min(ans, temp)
 
    # Print the answer
    print(ans)
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr =  [3, 4, 6, 7, 8, 9, 10]
     
    # Stores the size of the array
    N = len(arr)
    findMinimumNumber(arr, N)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static long MAX = 50;
 
// Function check if a
// number is prime or not
static bool isPrime(long n)
{
     
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // Check if n is divisible by 2 or 3
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    // Check for every 6th number. The above
    // checking allows to skip middle 5 numbers
    for(long i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Function to store primes in an array
static void findPrime(List primes)
{
    for(long i = 2; i <= MAX; i++)
    {
        if (isPrime(i))
            primes.Add(i);
    }
}
 
// Function to calculate
// GCD of two numbers
static long gcd(long a, long b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to find the smallest
// number which is not coprime with
// any element of the array arr[]
static void findMinimumNumber(long []arr, long N)
{
 
    List primes = new List();
     
    // Function call to fill
    // the prime numbers
    findPrime(primes);
 
    // Stores the answer
    long ans = 2147483647;
 
    int n = primes.Count;
 
    // Generate all non-empty
    // subsets of the primes[] array
    for(int i = 1; i < (1 << n); i++)
    {
         
        // Stores product of the primes
        long temp = 1;
        for(int j = 0; j < n; j++)
        {
            if ((i & (1 << j)) > 0)
            {
                temp *= primes[j];
            }
        }
 
        // Checks if temp is coprime
        // with the array or not
        bool check = true;
 
        // Check if the product temp is
        // not coprime with the whole array
        for(long k = 0; k < N; k++)
        {
            if (gcd(temp, arr[k]) == 1)
            {
                check = false;
                break;
            }
        }
 
        // If the product is not
        // co-prime with the array
        if (check == true)
            ans = Math.Min(ans, temp);
    }
 
    // Prlong the answer
    Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
     
    // Given array
    long []arr = { 3, 4, 6, 7, 8, 9, 10 };
     
    // Stores the size of the array
    long N = arr.Length;
     
    findMinimumNumber(arr, N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


输出:
42

时间复杂度: O(2 M * N * log(X)),其中M数组primes []的大小, X数组arr []中的最小元素
辅助空间: O(M)