给定一个正整数N> 1 。求和等于给定N的素数的最大计数。
例子:
Input : N = 5
Output : 2
Explanation : 2 and 3 are two prime numbers whose sum is 5.
Input : N = 6
Output :3
Explanation : 2, 2, 2 are three prime numbers whose sum is 6.
对于求和等于给定n的最大素数,素数必须尽可能小。因此,2是可能的最小质数,并且是偶数。下一个质数大于2的是3,这是奇数。因此,对于任何给定的n,都有两个条件,即n为奇数或偶数。
- 情况1:n是偶数,在这种情况下, n / 2将是答案(n / 2的2将成为n的总和)。
- 情况1:n是奇数,在这种情况下floor(n / 2)将是答案((n-3)/ 2 2的个数和一个3将成为n的和。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find max count of primes
int maxPrimes(int n)
{
// if n is even n/2 is required answer
// if n is odd floor(n/2) = (int)(n/2) is required answer
return n / 2;
}
// Driver Code
int main()
{
int n = 17;
cout << maxPrimes(n);
return 0;
}
Java
// Java program for above approach
class GFG
{
// Function to find max count of primes
static int maxPrimes(int n)
{
// if n is even n/2 is required answer
// if n is odd floor(n/2) = (int)(n/2)
// is required answer
return n / 2;
}
// Driver Code
public static void main(String[] args)
{
int n = 17;
System.out.println(maxPrimes(n));
}
}
// This code is contributed
// by Code_Mech
Python3
# Python3 program for above approach
# Function to find max count of primes
def maxPrmimes(n):
# if n is even n/2 is required answer
# if n is odd floor(n/2) = (int)(n/2)
# is required answer
return n // 2
# Driver code
n = 17
print(maxPrmimes(n))
# This code is contributed
# by Shrikant13
C#
// C# program for above approach
using System;
class GFG
{
// Function to find max count of primes
static int maxPrimes(int n)
{
// if n is even n/2 is required answer
// if n is odd floor(n/2) = (int)(n/2)
// is required answer
return n / 2;
}
// Driver Code
public static void Main()
{
int n = 17;
Console.WriteLine(maxPrimes(n));
}
}
// This code is contributed
// by Akanksha Rai
PHP
输出:
8