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 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 <bits/stdc++.h>using namespace std; // check if a number is prime or notbool 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 costint 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 codeint main(){ int n = 6; cout << minimumCost(n); return 0;} |
Java
// Java implementation of the approachimport java.util.*; class GFG{ // check if a number is prime or notstatic 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 coststatic 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 codepublic 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 approachusing System;public class GFG{ // check if a number is prime or notstatic 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 coststatic 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 codepublic static void Main(String []args){ int n = 6; Console.WriteLine(minimumCost(n));}} // This code is contributed by// Rajput-Ji |
PHP
<?php// PHP implementation of the approach// check if a number is prime or notfunction isPrime($x){ // run a loop upto square root of x for ($i = 2; $i * $i <= $x; $i++) { if ($x % $i == 0) return 0; } return 1;} // Function to return the minimized costfunction 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 && 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$n = 6;echo(minimumCost($n)); // This code is contributed by Code_Mech. |
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.


