Suma de todos los segundos divisores más grandes después de dividir un número en una o más partes

Dado un número entero N ( 2 <= N <= 10^9 ), divida el número en una o más partes (posiblemente ninguna), donde cada parte debe ser mayor que 1. La tarea es encontrar la suma mínima posible de la segunda divisor más grande de todos los números de división.
Ejemplos: 
 

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 

Enfoque: 
La idea se basa en la conjetura de Goldbach
 

  1. Cuando el número es primo, entonces la respuesta será 1.
  2. Cuando un número es par, siempre se puede expresar como una suma de 2 números primos. Entonces, la respuesta será 2.
  3. Cuando el número es impar, 
    • Cuando N-2 es primo, entonces el número se puede expresar como la suma de 2 primos, que son 2 y N-2, entonces la respuesta será 2.
    • De lo contrario, la respuesta siempre será 3.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// CPP program to find sum of all second largest divisor
// after splitting a number into one or more parts
#include <bits/stdc++.h>
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

<script>
// Javascript program to find sum of all second largest divisor
// after splitting a number into one or more parts
 
// Function to find a number is prime or not
function prime(n)
{
    if (n == 1)
        return false;
 
    // If there is any divisor
    for (let 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
function 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
    let n = 27;
 
    // Function call
    document.write(Min_Sum(n));
 
// This code is contributed by Mayank Tyagi
 
</script>
Producción: 

3

 

Complejidad del tiempo: O(sqrt(N))

Espacio Auxiliar: O(1)
 

Publicación traducida automáticamente

Artículo escrito por ajourney y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *