Minimize the cost to split a number
Last Updated :
09 Jun, 2022
Given an integer N ? 2, you can split the number as a sum of k integers i.e. N = k1 + k2 + … + kn where each kth element is ? 2 then the cost of splitting is calculated as maxDiv(k1) + maxDiv(k2) + … + maxDiv(kn) where maxDiv(x) is the maximum divisor of x which is < x.
The task is to split the number in such a way that the cost is minimized, print the minimized cost in the end.
Examples:
Input: N = 6
Output: 2
6 can be represented as (3 + 3) and the cost will be 1 + 1 = 2.
Input: N = 5
Output: 1
Approach:
- When n is prime, then the cost will be 1 as we don’t have to split n and the greatest divisor of n less than itself will be 1.
- If n is odd and n – 2 is prime, then n can be split into (2 + prime), which will cost 1 + 1 = 2.
- If n is even, then the cost will be 2 as Goldbach’s conjecture, every even number greater than 2 can be expressed as the sum of two primes, which is proven by 4 * 1018.
- If all of the above conditions are not satisfied, then n must be odd now and if 3 is subtracted from n then it will become even, which can be expressed as (3 + even) = (3 + prime + prime) which will cost 3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int x)
{
for ( int i = 2; i * i <= x; i++) {
if (x % i == 0)
return 0;
}
return 1;
}
int minimumCost( int n)
{
if (isPrime(n))
return 1;
if (n % 2 == 1 && isPrime(n - 2))
return 2;
if (n % 2 == 0)
return 2;
return 3;
}
int main()
{
int n = 6;
cout << minimumCost(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isPrime( int x)
{
for ( int i = 2 ; i * i <= x; i++)
{
if (x % i == 0 )
return false ;
}
return true ;
}
static int minimumCost( int n)
{
if (isPrime(n))
return 1 ;
if (n % 2 == 1 && isPrime(n - 2 ))
return 2 ;
if (n % 2 == 0 )
return 2 ;
return 3 ;
}
public static void main(String args[])
{
int n = 6 ;
System.out.println(minimumCost(n));
}
}
|
Python3
from math import sqrt
def isPrime(x) :
for i in range ( 2 , int (sqrt(x)) + 1 ) :
if (x % i = = 0 ) :
return 0 ;
return 1 ;
def minimumCost(n) :
if (isPrime(n)) :
return 1 ;
if (n % 2 = = 1 and isPrime(n - 2 )) :
return 2 ;
if (n % 2 = = 0 ) :
return 2 ;
return 3 ;
if __name__ = = "__main__" :
n = 6 ;
print (minimumCost(n));
|
C#
using System;
public class GFG
{
static bool isPrime( int x)
{
for ( int i = 2; i * i <= x; i++)
{
if (x % i == 0)
return false ;
}
return true ;
}
static int minimumCost( int n)
{
if (isPrime(n))
return 1;
if (n % 2 == 1 && isPrime(n - 2))
return 2;
if (n % 2 == 0)
return 2;
return 3;
}
public static void Main(String []args)
{
int n = 6;
Console.WriteLine(minimumCost(n));
}
}
|
PHP
<?php
function isPrime( $x )
{
for ( $i = 2; $i * $i <= $x ; $i ++)
{
if ( $x % $i == 0)
return 0;
}
return 1;
}
function minimumCost( $n )
{
if (isPrime( $n ))
return 1;
if ( $n % 2 == 1 && isPrime( $n - 2))
return 2;
if ( $n % 2 == 0)
return 2;
return 3;
}
$n = 6;
echo (minimumCost( $n ));
|
Javascript
<script>
function isPrime(x)
{
for (let i = 2; i * i <= x; i++)
{
if (x % i == 0)
return 0;
}
return 1;
}
function minimumCost(n)
{
if (isPrime(n))
return 1;
if (n % 2 == 1 && isPrime(n - 2))
return 2;
if (n % 2 == 0)
return 2;
return 3;
}
let n = 6;
document.write(minimumCost(n));
</script>
|
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...