📜  最小化拆分数字的成本

📅  最后修改于: 2021-06-25 19:02:37             🧑  作者: Mango

给定整数N≥2 ,您可以将数字拆分为k个整数的总和,即N = k1 + k2 +…+ kn ,其中每个第k个元素均≥2,则拆分成本计算为maxDiv(k1)+ maxDiv( K2)+ … + maxDiv(KN)其中maxDiv(x)的最大x的除数是

任务是按最小化成本的方式分割数字,最后打印最小化的成本。

例子:

方法:

  • n为质数时,成本将为1,因为我们不必拆分n,并且n小于其自身的最大除数将为1
  • 如果n为奇数n – 2为素数,则可以将n分解为(2 +素数) ,这将花费1 +1 = 2
  • 如果n是偶数,那么根据哥德巴赫的猜想,代价将为2 ,大于2的每个偶数都可以表示为两个素数之和,直到4 * 10 18为止。
  • 如果不满足上述所有条件,则n必须现在为奇数,如果从n中减去3 ,则它将变为偶数,可以表示为(3 +偶数)=(3 +素数+素数) ,这将花费3

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// check if a number is prime or not
bool isPrime(int x)
{
    // run a loop upto square root of x
    for (int i = 2; i * i <= x; i++) {
        if (x % i == 0)
            return 0;
    }
    return 1;
}
  
// Function to return the minimized cost
int minimumCost(int n)
{
    // If n is prime
    if (isPrime(n))
        return 1;
  
    // If n is odd and can be
    // split into (prime + 2)
    // then cost will be 1 + 1 = 2
    if (n % 2 == 1 && isPrime(n - 2))
        return 2;
  
    // Every non-prime even number
    // can be expressed as the
    // sum of two primes
    if (n % 2 == 0)
        return 2;
  
    // n is odd so n can be split into (3 + even)
    // further even part can be
    // split into (prime + prime)
    // (3 + prime + prime) will cost 3
    return 3;
}
  
// Driver code
int main()
{
    int n = 6;
    cout << minimumCost(n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// check if a number is prime or not
static boolean isPrime(int x)
{
    // run a loop upto square root of x
    for (int i = 2; i * i <= x; i++) 
    {
        if (x % i == 0)
            return false;
    }
    return true;
}
  
// Function to return the minimized cost
static int minimumCost(int n)
{
    // If n is prime
    if (isPrime(n))
        return 1;
  
    // If n is odd and can be
    // split into (prime + 2)
    // then cost will be 1 + 1 = 2
    if (n % 2 == 1 && isPrime(n - 2))
        return 2;
  
    // Every non-prime even number
    // can be expressed as the
    // sum of two primes
    if (n % 2 == 0)
        return 2;
  
    // n is odd so n can be split into (3 + even)
    // further even part can be
    // split into (prime + prime)
    // (3 + prime + prime) will cost 3
    return 3;
}
  
// Driver code
public static void main(String args[])
{
    int n = 6;
    System.out.println(minimumCost(n));
}
}
  
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the approach 
from math import sqrt
  
# check if a number is prime or not 
def isPrime(x) : 
  
    # run a loop upto square root of x 
    for i in range(2, int(sqrt(x)) + 1) :
        if (x % i == 0) :
            return 0; 
              
    return 1; 
  
# Function to return the minimized cost 
def minimumCost(n) :
  
    # If n is prime 
    if (isPrime(n)) :
        return 1; 
  
    # If n is odd and can be 
    # split into (prime + 2) 
    # then cost will be 1 + 1 = 2 
    if (n % 2 == 1 and isPrime(n - 2)) : 
        return 2; 
  
    # Every non-prime even number 
    # can be expressed as the 
    # sum of two primes 
    if (n % 2 == 0) :
        return 2; 
  
    # n is odd so n can be split into 
    # (3 + even) further even part can be 
    # split into (prime + prime) 
    # (3 + prime + prime) will cost 3 
    return 3; 
  
# Driver code 
if __name__ == "__main__" : 
  
    n = 6; 
    print(minimumCost(n)); 
      
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
public class GFG
{
   
// check if a number is prime or not
static bool isPrime(int x)
{
    // run a loop upto square root of x
    for (int i = 2; i * i <= x; i++) 
    {
        if (x % i == 0)
            return false;
    }
    return true;
}
   
// Function to return the minimized cost
static int minimumCost(int n)
{
    // If n is prime
    if (isPrime(n))
        return 1;
   
    // If n is odd and can be
    // split into (prime + 2)
    // then cost will be 1 + 1 = 2
    if (n % 2 == 1 && isPrime(n - 2))
        return 2;
   
    // Every non-prime even number
    // can be expressed as the
    // sum of two primes
    if (n % 2 == 0)
        return 2;
   
    // n is odd so n can be split into (3 + even)
    // further even part can be
    // split into (prime + prime)
    // (3 + prime + prime) will cost 3
    return 3;
}
   
// Driver code
public static void Main(String []args)
{
    int n = 6;
    Console.WriteLine(minimumCost(n));
}
}
   
// This code is contributed by
// Rajput-Ji


PHP


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。