Related Articles
Minimize the cost to split a number
• Difficulty Level : Hard
• Last Updated : 24 May, 2019

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 according to Goldbach’s conjecture, every even number greater than 2 can be expressed as sum of two primes which is proven till 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++

 `// 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

 `
Output:
```2
```

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up