Sum of all second largest divisors after splitting a number into one or more parts
Given an integer N( 2 <= N <= 10^9 ), split the number into one or more parts(possibly none), where each part must be greater than 1. The task is to find the minimum possible sum of the second largest divisor of all the splitting numbers.
Examples:
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
Approach:
The idea is based on Goldbach’s conjecture.
- When the number is prime, then the answer will be 1.
- When a number is even then it can always be expressed as a sum of 2 primes. So, the answer will be 2.
- When the number is odd,
- When N-2 is prime, then the number can be express as the sum of 2 primes, that are 2 and N-2, then the answer will be 2.
- Otherwise, the answer will always be 3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool prime( int n)
{
if (n == 1)
return false ;
for ( int i = 2; i * i <= n; ++i)
if (n % i == 0)
return false ;
return true ;
}
int Min_Sum( int n)
{
if (prime(n))
return 1;
if (n % 2 == 0)
return 2;
else {
if (prime(n - 2))
return 2;
else
return 3;
}
}
int main()
{
int n = 27;
cout << Min_Sum(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean prime( int n)
{
if (n == 1 )
return false ;
for ( int i = 2 ; i * i <= n; ++i)
if (n % i == 0 )
return false ;
return true ;
}
static int Min_Sum( int n)
{
if (prime(n))
return 1 ;
if (n % 2 == 0 )
return 2 ;
else {
if (prime(n - 2 ))
return 2 ;
else
return 3 ;
}
}
public static void main (String[] args) {
int n = 27 ;
System.out.println( Min_Sum(n));
}
}
|
Python3
from math import sqrt
def prime(n):
if (n = = 1 ):
return False
for i in range ( 2 , int (sqrt(n)) + 1 , 1 ):
if (n % i = = 0 ):
return False
return True
def Min_Sum(n):
if (prime(n)):
return 1
if (n % 2 = = 0 ):
return 2
else :
if (prime(n - 2 )):
return 2
else :
return 3
if __name__ = = '__main__' :
n = 27
print (Min_Sum(n))
|
C#
using System;
class GFG
{
static bool prime( int n)
{
if (n == 1)
return false ;
for ( int i = 2; i * i <= n; ++i)
if (n % i == 0)
return false ;
return true ;
}
static int Min_Sum( int n)
{
if (prime(n))
return 1;
if (n % 2 == 0)
return 2;
else {
if (prime(n - 2))
return 2;
else
return 3;
}
}
public static void Main ()
{
int n = 27;
Console.WriteLine( Min_Sum(n));
}
}
|
Javascript
<script>
function prime(n)
{
if (n == 1)
return false ;
for (let i = 2; i * i <= n; ++i)
if (n % i == 0)
return false ;
return true ;
}
function Min_Sum(n)
{
if (prime(n))
return 1;
if (n % 2 == 0)
return 2;
else {
if (prime(n - 2))
return 2;
else
return 3;
}
}
let n = 27;
document.write(Min_Sum(n));
</script>
|
Time complexity: O(sqrt(N))
Auxiliary Space: O(1)
Last Updated :
07 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...