Monday, November 18, 2024
Google search engine
HomeData Modelling & AIMinimum number of primes required such that their sum is equal to...

Minimum number of primes required such that their sum is equal to N

Given a positive integer N greater than 1, the task is to find the minimum count of Prime Numbers whose sum is equal to given N.
Examples: 

Input: N = 100 
Output:
Explanation: 
100 can be written as sum of 2 prime numbers 97 and 3.

Input: N = 25 
Output:
Explanation: 
25 can be written as sum of 2 prime numbers  23 and 2.

Approach: 
For the minimum number of primes whose sum is the given number N, Prime Numbers must be as large as possible. Following are the observation for the above problem statement: 

  • Case 1: If the number is prime, then the minimum primes numbers required to make sum N is 1.
  • Case 2: If the number is even, then it can be expressed as a sum of two primes as per the Goldbach’s Conjecture for every even integer greater than 2. Therefore the minimum prime number required to make the sum N is 2.
  • Case 3: If the number is odd: 
    1. If (N-2) is prime, then the minimum prime number required to make the given sum N is 2.
    2. Else The minimum prime numbers required to make the given sum N is 3 because:
As N is odd, then (N - 3) is even.
Hence As per case 2:
The minimum prime number required to make the sum (N-3) is 2.
Therefore,
The minimum prime number required to make the sum N is 3(2+1).

Below are the steps:

  1. Check whether the given number N is prime or not, by using the approach discussed in this article. If Yes then print 1.
  2. Else as per the above Cases print the minimum number of Prime Numbers required to make the given sum N.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if n is prime
bool isPrime(int n)
{
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
// Function to count the minimum
// prime required for given sum N
void printMinCountPrime(int N)
{
 
    int minCount;
 
    // Case 1:
    if (isPrime(N)) {
        minCount = 1;
    }
 
    // Case 2:
    else if (N % 2 == 0) {
        minCount = 2;
    }
 
    // Case 3:
    else {
 
        // Case 3a:
        if (isPrime(N - 2)) {
            minCount = 2;
        }
 
        // Case 3b:
        else {
            minCount = 3;
        }
    }
 
    cout << minCount << endl;
}
 
// Driver Code
int main()
{
    int N = 100;
 
    // Function Call
    printMinCountPrime(N);
    return 0;
}


Java




// Java program for the above approach
class GFG{
 
// Function to check if n is prime
static boolean isPrime(int n)
{
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
// Function to count the minimum
// prime required for given sum N
static void printMinCountPrime(int N)
{
 
    int minCount;
 
    // Case 1:
    if (isPrime(N)) {
        minCount = 1;
    }
 
    // Case 2:
    else if (N % 2 == 0) {
        minCount = 2;
    }
 
    // Case 3:
    else {
 
        // Case 3a:
        if (isPrime(N - 2)) {
            minCount = 2;
        }
 
        // Case 3b:
        else {
            minCount = 3;
        }
    }
 
    System.out.print(minCount +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 100;
 
    // Function Call
    printMinCountPrime(N);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 program for the above approach
 
# Function to check if n is prime
def isPrime(n) :
 
    for i in range(2, int(n ** (1/2)) + 1) :
        if (n % i == 0) :
            return False;
     
    return True;
 
# Function to count the minimum
# prime required for given sum N
def printMinCountPrime(N) :
 
    # Case 1:
    if (isPrime(N)) :
        minCount = 1;
 
    # Case 2:
    elif (N % 2 == 0) :
        minCount = 2;
 
    # Case 3:
    else :
 
        # Case 3a:
        if (isPrime(N - 2)) :
            minCount = 2;
 
        # Case 3b:
        else :
            minCount = 3;
 
    print(minCount) ;
 
# Driver Code
if __name__ == "__main__" :
    N = 100;
 
    # Function Call
    printMinCountPrime(N);
 
# This code is contributed by AnkitRai01


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if n is prime
static bool isPrime(int n)
{
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
// Function to count the minimum
// prime required for given sum N
static void printMinCountPrime(int N)
{
 
    int minCount;
 
    // Case 1:
    if (isPrime(N)) {
        minCount = 1;
    }
 
    // Case 2:
    else if (N % 2 == 0) {
        minCount = 2;
    }
 
    // Case 3:
    else {
 
        // Case 3a:
        if (isPrime(N - 2)) {
            minCount = 2;
        }
 
        // Case 3b:
        else {
            minCount = 3;
        }
    }
 
    Console.WriteLine(minCount +"\n");
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 100;
 
    // Function Call
    printMinCountPrime(N);
}
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
// JavaScript program for the above approach
 
 
// Function to check if n is prime
function isPrime(n)
{
    for (let i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
// Function to count the minimum
// prime required for given sum N
function printMinCountPrime(N)
{
 
    let minCount;
 
    // Case 1:
    if (isPrime(N)) {
        minCount = 1;
    }
 
    // Case 2:
    else if (N % 2 == 0) {
        minCount = 2;
    }
 
    // Case 3:
    else {
 
        // Case 3a:
        if (isPrime(N - 2)) {
            minCount = 2;
        }
 
        // Case 3b:
        else {
            minCount = 3;
        }
    }
 
    document.write(minCount + "<br>");
}
 
// Driver Code
 
let N = 100;
 
// Function Call
printMinCountPrime(N);
 
 
// This code is contributed by gfgking
 
</script>


Output: 

2

 

Time Complexity: O(?N), where N is the given number.
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments