📌  相关文章
📜  总和等于给定N的最大素数

📅  最后修改于: 2021-04-26 08:08:53             🧑  作者: Mango

给定一个正整数N> 1 。求和等于给定N的素数的最大计数。

例子:

对于求和等于给定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