📜  数组中每个数字的最大和最小素因数之和

📅  最后修改于: 2021-09-06 11:30:21             🧑  作者: Mango

给定一个数组ARR [],任务是找到最大和给定的阵列中的每个号码的最小素因子
例子:

方法:想法是使用埃拉托色尼筛法预先计算每个数字的所有最小和最大素因数,并将其存储在两个数组中。经过这个预计算,可以在常数时间内找到最小和最大素数的总和。
下面是上述方法的实现:

CPP
// C++ implementation of the approach
 
#include 
using namespace std;
 
const int MAX = 100000;
 
// max_prime[i] represent maximum prime
// number that divides the number i
int max_prime[MAX];
 
// min_prime[i] represent minimum prime
// number that divides the number i
int min_prime[MAX];
 
// Function to store the minimum prime factor
// and the maximum prime factor in two arrays
void sieve(int n)
{
    for (int i = 2; i <= n; ++i) {
 
        // Check for prime number
        // if min_prime[i]>0,
        // then it is not a prime number
        if (min_prime[i] > 0) {
            continue;
        }
 
        // if i is a prime number
        // min_prime number that divide prime number
        // and max_prime number that divide prime number
        // is the number itself.
        min_prime[i] = i;
        max_prime[i] = i;
 
        int j = i + i;
 
        while (j <= n) {
            if (min_prime[j] == 0) {
 
                // If this number is being visited
                // for first time then this divisor
                // must be the smallest prime number
                // that divides this number
                min_prime[j] = i;
            }
 
            // Update prime number till
            // last prime number that divides this number
 
            // The last prime number that
            // divides this number will be maximum.
            max_prime[j] = i;
            j += i;
        }
    }
}
 
// Function to find the sum of the minimum
// and the maximum prime factors of every
// number from the given array
void findSum(int arr[], int n)
{
 
    // Pre-calculation
    sieve(MAX);
 
    // For every element of the given array
    for (int i = 0; i < n; i++) {
 
        // The sum of its smallest
        // and largest prime factor
        int sum = min_prime[arr[i]]
                  + max_prime[arr[i]];
 
        cout << sum << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 5, 10, 15, 20, 25, 30 };
    int n = sizeof(arr) / sizeof(int);
 
    findSum(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    static int MAX = 100000;
     
    // max_prime[i] represent maximum prime
    // number that divides the number i
    static int max_prime[] = new int[MAX + 1];
     
    // min_prime[i] represent minimum prime
    // number that divides the number i
    static int min_prime[] = new int[MAX + 1];
     
    // Function to store the minimum prime factor
    // and the maximum prime factor in two arrays
    static void sieve(int n)
    {
        for (int i = 2; i <= n; ++i)
        {
     
            // Check for prime number
            // if min_prime[i] > 0,
            // then it is not a prime number
            if (min_prime[i] > 0)
            {
                continue;
            }
     
            // if i is a prime number
            // min_prime number that divide prime number
            // and max_prime number that divide prime number
            // is the number itself.
            min_prime[i] = i;
            max_prime[i] = i;
     
            int j = i + i;
     
            while (j <= n)
            {
                if (min_prime[j] == 0)
                {
     
                    // If this number is being visited
                    // for first time then this divisor
                    // must be the smallest prime number
                    // that divides this number
                    min_prime[j] = i;
                }
     
                // Update prime number till
                // last prime number that divides this number
     
                // The last prime number that
                // divides this number will be maximum.
                max_prime[j] = i;
                j += i;
            }
        }
    }
     
    // Function to find the sum of the minimum
    // and the maximum prime factors of every
    // number from the given array
    static void findSum(int arr[], int n)
    {
     
        // Pre-calculation
        sieve(MAX);
     
        // For every element of the given array
        for (int i = 0; i < n; i++)
        {
     
            // The sum of its smallest
            // and largest prime factor
            int sum = min_prime[arr[i]]
                    + max_prime[arr[i]];
     
            System.out.print(sum + " ");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 5, 10, 15, 20, 25, 30 };
        int n = arr.length ;
     
        findSum(arr, n);
     
    }
}
 
// This code is contributed by AnkitRai01


Python
# Python3 implementation of the approach
MAX = 100000
 
# max_prime[i] represent maximum prime
# number that divides the number i
max_prime = [0]*(MAX + 1)
 
# min_prime[i] represent minimum prime
# number that divides the number i
min_prime = [0]*(MAX + 1)
 
# Function to store the minimum prime factor
# and the maximum prime factor in two arrays
def sieve(n):
    for i in range(2, n + 1):
 
        # Check for prime number
        # if min_prime[i]>0,
        # then it is not a prime number
        if (min_prime[i] > 0):
            continue
 
        # if i is a prime number
        # min_prime number that divide prime number
        # and max_prime number that divide prime number
        # is the number itself.
        min_prime[i] = i
        max_prime[i] = i
 
        j = i + i
 
        while (j <= n):
            if (min_prime[j] == 0):
 
                # If this number is being visited
                # for first time then this divisor
                # must be the smallest prime number
                # that divides this number
                min_prime[j] = i
 
            # Update prime number till
            # last prime number that divides this number
 
            # The last prime number that
            # divides this number will be maximum.
            max_prime[j] = i
            j += i
 
# Function to find the sum of the minimum
# and the maximum prime factors of every
# number from the given array
def findSum(arr, n):
 
    # Pre-calculation
    sieve(MAX)
 
    # For every element of the given array
    for i in range(n):
 
        # The sum of its smallest
        # and largest prime factor
        sum = min_prime[arr[i]] + max_prime[arr[i]]
 
        print(sum, end = " ")
 
# Driver code
arr = [5, 10, 15, 20, 25, 30]
n = len(arr)
 
findSum(arr, n)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
 
class GFG
{
      
    static int MAX = 100000;
      
    // max_prime[i] represent maximum prime
    // number that divides the number i
    static int []max_prime = new int[MAX + 1];
      
    // min_prime[i] represent minimum prime
    // number that divides the number i
    static int []min_prime = new int[MAX + 1];
      
    // Function to store the minimum prime factor
    // and the maximum prime factor in two arrays
    static void sieve(int n)
    {
        for (int i = 2; i <= n; ++i)
        {
      
            // Check for prime number
            // if min_prime[i] > 0,
            // then it is not a prime number
            if (min_prime[i] > 0)
            {
                continue;
            }
      
            // if i is a prime number
            // min_prime number that divide prime number
            // and max_prime number that divide prime number
            // is the number itself.
            min_prime[i] = i;
            max_prime[i] = i;
      
            int j = i + i;
      
            while (j <= n)
            {
                if (min_prime[j] == 0)
                {
      
                    // If this number is being visited
                    // for first time then this divisor
                    // must be the smallest prime number
                    // that divides this number
                    min_prime[j] = i;
                }
      
                // Update prime number till
                // last prime number that divides this number
      
                // The last prime number that
                // divides this number will be maximum.
                max_prime[j] = i;
                j += i;
            }
        }
    }
      
    // Function to find the sum of the minimum
    // and the maximum prime factors of every
    // number from the given array
    static void findSum(int []arr, int n)
    {
      
        // Pre-calculation
        sieve(MAX);
      
        // For every element of the given array
        for (int i = 0; i < n; i++)
        {
      
            // The sum of its smallest
            // and largest prime factor
            int sum = min_prime[arr[i]]
                    + max_prime[arr[i]];
      
            Console.Write(sum + " ");
        }
    }
      
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 5, 10, 15, 20, 25, 30 };
        int n = arr.Length ;
      
        findSum(arr, n);
      
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
10 7 8 7 10 7

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live