📌  相关文章
📜  将数字分成一个或多个部分后,所有第二大除数的总和

📅  最后修改于: 2021-05-08 18:13:35             🧑  作者: Mango

给定整数N (2 <= N <= 10 ^ 9),请将数字分成一个或多个部分(可能没有一个),其中每个部分都必须大于1。任务是找到第二个部分的最小和。所有分割数的最大除数。
例子:

Input : N = 27 
Output : 3
Explanation : Split the given number into 19, 5, 3. Second largest 
divisor of each number is 1. So, sum is 3.

Input : N = 19
Output : 1
Explanation : Don't make any splits. Second largest divisor of 19 
is 1. So, sum is 1 

方法:
这个想法是基于哥德巴赫的猜想。

  1. 当数字为质数时,答案为1。
  2. 当一个数为偶数时,它总是可以表示为2个质数之和。因此,答案将是2。
  3. 当数字为奇数时
    • 当N-2为质数时,该数字可以表示为2个质数之和,即2和N-2,则答案为2。
    • 否则,答案将始终为3。

下面是上述方法的实现:

C++
// CPP program to find sum of all second largest divisor
// after splitting a number into one or more parts
#include 
using namespace std;
 
// Function to find a number is prime or not
bool prime(int n)
{
    if (n == 1)
        return false;
 
    // If there is any divisor
    for (int i = 2; i * i <= n; ++i)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Function to find the sum of all second largest divisor
// after splitting a number into one or more parts
int Min_Sum(int n)
{
    // If number is prime
    if (prime(n))
        return 1;
 
    // If n is even
    if (n % 2 == 0)
        return 2;
 
    // If the number is odd
    else {
 
        // If N-2 is prime
        if (prime(n - 2))
            return 2;
 
        // There exists 3 primes x1, x2, x3
        // such that x1 + x2 + x3 = n
        else
            return 3;
    }
}
 
// Driver code
int main()
{
    int n = 27;
 
    // Function call
    cout << Min_Sum(n);
 
    return 0;
}


Java
// Java program to Sum of all second largest
// divisors after splitting a number into one or more parts
import java.io.*;
  
class GFG {
  
  
  
// Function to find a number is prime or not
static boolean prime(int n)
{
    if (n == 1)
        return false;
  
    // If there is any divisor
    for (int i = 2; i * i <= n; ++i)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// Function to find the sum of all second largest divisor
// after splitting a number into one or more parts
static int Min_Sum(int n)
{
    // If number is prime
    if (prime(n))
        return 1;
  
    // If n is even
    if (n % 2 == 0)
        return 2;
  
    // If the number is odd
    else {
  
        // If N-2 is prime
        if (prime(n - 2))
            return 2;
  
        // There exists 3 primes x1, x2, x3
        // such that x1 + x2 + x3 = n
        else
            return 3;
    }
}
  
// Driver code
  
  
    public static void main (String[] args) {
    int n = 27;
  
    // Function call
    System.out.println( Min_Sum(n));
    }
}
 
// This code is contributed by anuj_6


Python3
# Python 3 program to find sum of all second largest divisor
# after splitting a number into one or more parts
 
from math import sqrt
# Function to find a number is prime or not
def prime(n):
    if (n == 1):
        return False
 
    # If there is any divisor
    for i in range(2,int(sqrt(n))+1,1):
        if (n % i == 0):
            return False
 
    return True
 
# Function to find the sum of all second largest divisor
# after splitting a number into one or more parts
def Min_Sum(n):
    # If number is prime
    if (prime(n)):
        return 1
 
    # If n is even
    if (n % 2 == 0):
        return 2
 
    # If the number is odd
    else:
        # If N-2 is prime
        if (prime(n - 2)):
            return 2
 
        # There exists 3 primes x1, x2, x3
        # such that x1 + x2 + x3 = n
        else:
            return 3
 
# Driver code
if __name__ == '__main__':
    n = 27
 
    # Function call
    print(Min_Sum(n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to Sum of all second largest
// divisors after splitting a number into one or more parts
using System;
 
class GFG
{
 
// Function to find a number is prime or not
static bool prime(int n)
{
    if (n == 1)
        return false;
 
    // If there is any divisor
    for (int i = 2; i * i <= n; ++i)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Function to find the sum of all second largest divisor
// after splitting a number into one or more parts
static int Min_Sum(int n)
{
    // If number is prime
    if (prime(n))
        return 1;
 
    // If n is even
    if (n % 2 == 0)
        return 2;
 
    // If the number is odd
    else {
 
        // If N-2 is prime
        if (prime(n - 2))
            return 2;
 
        // There exists 3 primes x1, x2, x3
        // such that x1 + x2 + x3 = n
        else
            return 3;
    }
}
 
// Driver code
public static void Main ()
{
    int n = 27;
 
    // Function call
    Console.WriteLine( Min_Sum(n));
}
}
 
// This code is contributed by anuj_6


Javascript


输出:
3

时间复杂度: O(sqrt(N))