Open In App

Minimize the cost to split a number

Improve
Improve
Like Article
Like
Save
Share
Report

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:
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++




// C++ implementation of the approach
#include <bits/stdc++.h>
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




<?php
// PHP implementation of the approach
// check if a number is prime or not
function 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 cost
function 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.


Javascript




<script>
// Javascript implementation of the approach
// check if a number is prime or not
function isPrime(x)
{
    // run a loop upto square root of x
    for (let i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
            return 0;
    }
    return 1;
}
 
// Function to return the minimized cost
function 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
let n = 6;
document.write(minimumCost(n));
 
// This code is contributed by _saurabh_jaiswal.
 
</script>


Output: 

2

 

Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)



Last Updated : 09 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads