Minimum number of single digit primes required whose sum is equal to N

Find the minimum number of single-digit prime numbers required whose sum will be equal to N.

Examples:

Input: 11
Output: 3
Explanation: 5 + 3 + 3.
Another possibility is 3 + 3 + 3 + 2, but it is not
the minimal

Input: 12
Output: 2
Explanation: 7 + 5

Approach: Dynamic Programming can be used to solve the above problem. The observations are:



  • There are only 4 single digit primes {2, 3, 5, 7}.
  • If it is possible to make N from summing up single digit primes, then at least one of N-2, N-3, N-5 or N-7, is also reachable.
  • The minimum number of single-digit prime numbers needed will be one more than the minimum number of prime digits needed to make one of {N-2, N-3, N-5, N-7}.

Using these observations, built a recurrence to solve this problem. The recurrence will be:

dp[i] = 1 + min(dp[i-2], dp[i-3], dp[i-5], dp[i-7])

For {2, 3, 5, 7}, the answer would be 1. For each other number, using Observation 3, try to find the minimum value possible, if possible.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the minimum number of single
// digit prime numbers required which when summed
// equals to a given number N.
#include <bits/stdc++.h>
using namespace std;
  
// fuction to check if i-th
// index is valid or not
bool check(int i, int val)
{
    if (i - val < 0)
        return false;
    return true;
}
  
// function to find the minimum number of single
// digit prime numbers required which when summed up
// equals to a given number N.
int MinimumPrimes(int n)
{
    int dp[n + 1];
  
    for (int i = 1; i <= n; i++)
        dp[i] = 1e9;
  
    dp[0] = dp[2] = dp[3] = dp[5] = dp[7] = 1;
    for (int i = 1; i <= n; i++) {
  
        if (check(i, 2))
            dp[i] = min(dp[i], 1 + dp[i - 2]);
  
        if (check(i, 3))
            dp[i] = min(dp[i], 1 + dp[i - 3]);
  
        if (check(i, 5))
            dp[i] = min(dp[i], 1 + dp[i - 5]);
  
        if (check(i, 7))
            dp[i] = min(dp[i], 1 + dp[i - 7]);
    }
  
    // Not possible
    if (dp[n] == (1e9))
        return -1;
    else
        return dp[n];
}
  
// Driver Code
int main()
{
  
    int n = 12;
  
    int minimal = MinimumPrimes(n);
    if (minimal != -1)
        cout << "Minimum number of single"
             << " digit primes required : "
             << minimal << endl;
  
    else
        cout << "Not possible";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the minimum number
// of single digit prime numbers required 
// which when summed equals to a given 
// number N.
  
class Geeks {
      
// fuction to check if i-th
// index is valid or not
static boolean check(int i, int val)
{
    if (i - val < 0)
        return false;
    else
        return true;
}
  
// function to find the minimum number
// of single digit prime numbers required
// which when summed up equals to a given
// number N.
static double MinimumPrimes(int n)
{
    double[] dp;
    dp = new double[n+1];
  
    for (int i = 1; i <= n; i++)
        dp[i] = 1e9;
  
    dp[0] = dp[2] = dp[3] = dp[5] = dp[7] = 1;
    for (int i = 1; i <= n; i++) {
  
        if (check(i, 2))
            dp[i] = Math.min(dp[i], 1 + dp[i - 2]);
  
        if (check(i, 3))
            dp[i] = Math.min(dp[i], 1 + dp[i - 3]);
  
        if (check(i, 5))
            dp[i] = Math.min(dp[i], 1 + dp[i - 5]);
  
        if (check(i, 7))
            dp[i] = Math.min(dp[i], 1 + dp[i - 7]);
    }
  
    // Not possible
    if (dp[n] == (1e9))
        return -1;
    else
        return dp[n];
}
  
    // Driver Code
    public static void main(String args[])
    {
      
        int n = 12;
        int minimal = (int)MinimumPrimes(n);
          
        if (minimal != -1)
            System.out.println("Minimum number of single "
                        "digit primes required: "+minimal);
      
        else
            System.out.println("Not Possible");
      
    }
}
  
// This code is contributed ankita_saini

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the minimum number 
# of single digit prime numbers required  
# which when summed equals to a given  
# number N.
  
# function to check if i-th 
# index is valid or not 
  
def check(i,val):
    if i-val<0:
        return False
    return True
  
# function to find the minimum number of single 
# digit prime numbers required which when summed up 
# equals to a given number N.
  
def MinimumPrimes(n):
    dp=[10**9]*(n+1)
    dp[0]=dp[2]=dp[3]=dp[5]=dp[7]=1
    for i in range(1,n+1):
        if check(i,2):
            dp[i]=min(dp[i],1+dp[i-2])
        if check(i,3):
            dp[i]=min(dp[i],1+dp[i-3])
        if check(i,5):
            dp[i]=min(dp[i],1+dp[i-5])
        if check(i,7):
            dp[i]=min(dp[i],1+dp[i-7])
  
    # Not possible
    if dp[n]==10**9:
        return -1
    else:
        return dp[n]
  
  
# Driver Code
if __name__ == "__main__":
    n=12
    minimal=MinimumPrimes(n)
    if minimal!=-1:
        print("Minimum number of single digit primes required : ",minimal)
    else:
        print("Not possible")
#This code is contributed Saurabh Shukla

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the 
// minimum number of single
// digit prime numbers required 
// which when summed equals to 
// a given number N.
using System;
  
class GFG 
{
      
// fuction to check if i-th
// index is valid or not
static Boolean check(int i, 
                     int val)
{
    if (i - val < 0)
        return false;
    else
        return true;
}
  
// function to find the 
// minimum number of single 
// digit prime numbers 
// required which when summed 
// up equals to a given
// number N.
static double MinimumPrimes(int n)
{
    double[] dp;
    dp = new double[n + 1];
  
    for (int i = 1; i <= n; i++)
        dp[i] = 1e9;
  
    dp[0] = dp[2] = dp[3] = dp[5] = dp[7] = 1;
    for (int i = 1; i <= n; i++) 
    {
        if (check(i, 2))
            dp[i] = Math.Min(dp[i], 1 + 
                             dp[i - 2]);
  
        if (check(i, 3))
            dp[i] = Math.Min(dp[i], 1 + 
                             dp[i - 3]);
  
        if (check(i, 5))
            dp[i] = Math.Min(dp[i], 1 + 
                             dp[i - 5]);
  
        if (check(i, 7))
            dp[i] = Math.Min(dp[i], 1 + 
                             dp[i - 7]);
    }
  
    // Not possible
    if (dp[n] == (1e9))
        return -1;
    else
        return dp[n];
}
  
// Driver Code
public static void Main(String []args)
{
    int n = 12;
    int minimal = (int)MinimumPrimes(n);
      
    if (minimal != -1)
        Console.WriteLine("Minimum number of single "
                            "digit primes required: "
                                              minimal);
    else
        Console.WriteLine("Not Possible");
  
}
}
  
// This code is contributed 
// by Ankita_Saini

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the minimum 
// number of single digit prime 
// numbers required which when summed
// equals to a given number N.
  
// fuction to check if i-th
// index is valid or not
function check($i, $val)
{
    if ($i - $val < 0)
        return false;
    return true;
}
  
// function to find the minimum 
// number of single digit prime 
// numbers required which when 
// summed up equals to a given
// number N.
function MinimumPrimes($n)
{
  
    for ($i = 1; $i<= $n; $i++)
        $dp[$i] = 1e9;
  
    $dp[0] = $dp[2] = $dp[3] = $dp[5] = $dp[7] = 1;
    for ($i = 1; $i <= $n; $i++) 
    {
  
        if (check($i, 2))
            $dp[$i] = min($dp[$i], 1 + 
                          $dp[$i - 2]);
  
        if (check($i, 3))
            $dp[$i] = min($dp[$i], 1 + 
                          $dp[$i - 3]);
  
        if (check($i, 5))
            $dp[$i] = min($dp[$i], 1 + 
                          $dp[$i - 5]);
  
        if (check($i, 7))
            $dp[$i] = min($dp[$i], 1 +
                          $dp[$i - 7]);
    }
  
    // Not possible
    if ($dp[$n] == (1e9))
        return -1;
    else
        return $dp[$n];
}
  
// Driver Code
$n = 12;
  
$minimal = MinimumPrimes($n);
if ($minimal != -1)
{
    echo("Minimum number of single "
           "digit primes required :"); 
    echo( $minimal );
}
else
{
    echo("Not possible");
}
  
// This code is contributed 
// by Shivi_Aggarwal
?>

chevron_right


Output:

Minimum number of single digit primes required : 2

Time Complexity: O(N)

Note: In case of multiple queries, the dp[] array can be pre-computed and we can answer every query in O(1).



My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.