📜  计算不超过3个素数的素数三元组,使得两个素数之和等于第三个素数

📅  最后修改于: 2021-04-17 13:51:38             🧑  作者: Mango

给定整数N ,任务是计算范围[1,N]中不同素数三元组(a,b,c)的数量,以使a a + b = c
注意:如果两个素元组中存在的素数中的至少一个不同,则它们是不同的。

例子:

方法:可以根据以下观察到的问题解决问题:

观察:

请按照以下步骤解决问题:

  • 初始化一个变量,例如count = 0 ,以存储主要三元组的数量。
  • 1N进行迭代,对于每个数字p ,检查此数字pp – 2是否为质数。
  • 如果它们是素数,则将count1
  • 打印count的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if a
// number is a prime or not
bool isPrime(int N)
{
    if (N <= 1)
        return false;
 
    for (int i = 2; i <= sqrt(N); i++) {
        if (N % i == 0)
            return false;
    }
 
    return true;
}
 
// Function to count the number
// of valid prime triplets
void countPrimeTuples(int N)
{
    // Stores the count
    // of prime triplets
    int count = 0;
 
    // Iterate from 2 to N and check for each
    // p, whether p & (p - 2) are prime or not
    for (int i = 2; i <= N; i++) {
 
        if (isPrime(i) && isPrime(i - 2))
            count++;
    }
 
    // Print the count obtained
    cout << count;
}
 
// Driver Code
int main()
{
    int N = 6;
    countPrimeTuples(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
  // Function to check if a
  // number is a prime or not
  static boolean isPrime(int N)
  {
      if (N <= 1)
          return false;
      for (int i = 2; i <= Math.sqrt(N); i++)
      {
          if (N % i == 0)
              return false;
      }
      return true;
  }
 
  // Function to count the number
  // of valid prime triplets
  static void countPrimeTuples(int N)
  {
     
      // Stores the count
      // of prime triplets
      int count = 0;
 
      // Iterate from 2 to N and check for each
      // p, whether p & (p - 2) are prime or not
      for (int i = 2; i <= N; i++)
      {
          if (isPrime(i) && isPrime(i - 2))
              count++;
      }
 
      // Print the count obtained
      System.out.println(count);
  }
 
  // Driver Code
  public static void main (String[] args)
  { 
    int N = 6;
    countPrimeTuples(N);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
import math
 
# Function to check if a
# number is a prime or not
def isPrime(N) :
    if (N <= 1) :
        return False
    for i in range(2, int(math.sqrt(N) + 1)):
        if (N % i == 0) :
            return False
    return True
 
# Function to count the number
# of valid prime triplets
def countPrimeTuples(N) :
     
    # Stores the count
    # of prime triplets
    count = 0
 
    # Iterate from 2 to N and check for each
    # p, whether p & (p - 2) are prime or not
    for i in range(2, N + 1):
 
        if (isPrime(i) and isPrime(i - 2)) :
            count += 1
     
    # Prthe count obtained
    print(count)
 
# Driver Code
N = 6
countPrimeTuples(N)
 
# This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
using System;
public class GFG
{
     
  // Function to check if a
  // number is a prime or not
  static bool isPrime(int N)
  {
      if (N <= 1)
          return false;
      for (int i = 2; i <= Math.Sqrt(N); i++)
      {
          if (N % i == 0)
              return false;
      }
      return true;
  }
 
  // Function to count the number
  // of valid prime triplets
  static void countPrimeTuples(int N)
  {
     
      // Stores the count
      // of prime triplets
      int count = 0;
 
      // Iterate from 2 to N and check for each
      // p, whether p & (p - 2) are prime or not
      for (int i = 2; i <= N; i++)
      {
          if (isPrime(i) && isPrime(i - 2))
              count++;
      }
 
      // Print the count obtained
      Console.WriteLine(count);
  }
 
  // Driver Code
  static public void Main ()
  {
    int N = 6;
    countPrimeTuples(N);
  }
}
 
// This code is contributed by Dharanendra L V.


输出:
1

时间复杂度: O(N 3/2 )
辅助空间: O(1)