📌  相关文章
📜  找到给定总和的三个素数

📅  最后修改于: 2021-05-07 10:16:53             🧑  作者: Mango

给定一个整数N ,任务是找到三个素数XYZ ,使得这三个数的总和等于N,X + Y + Z = N。

例子:

方法:

  • 使用Eratosthenes筛生成质数
  • 从第一个素数开始。
  • 从生成的列表中选取另一个数字。
  • 从原始编号中减去第一个编号和第二个编号以获得第三个编号。
  • 检查第三个数字是否是质数。
  • 如果第三个数字是质数,则输出这三个数字。
  • 否则,对第二个数字重复此过程,因此对第一个数字重复此过程
  • 如果答案不存在,则打印-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
const int MAX = 100001;
 
// The vector primes holds
// the prime numbers
vector primes;
 
// Function to generate prime numbers
void initialize()
{
 
    // Initialize the array elements to 0s
    bool numbers[MAX] = {};
    int n = MAX;
    for (int i = 2; i * i <= n; i++)
        if (!numbers[i])
            for (int j = i * i; j <= n; j += i)
 
                // Set the non-primes to true
                numbers[j] = true;
 
    // Fill the vector primes with prime
    // numbers which are marked as false
    // in the numbers array
    for (int i = 2; i <= n; i++)
        if (numbers[i] == false)
            primes.push_back(i);
}
 
// Function to print three prime numbers
// which sum up to the number N
void findNums(int num)
{
 
    bool ans = false;
    int first = -1, second = -1, third = -1;
    for (int i = 0; i < num; i++) {
 
        // Take the first prime number
        first = primes[i];
        for (int j = 0; j < num; j++) {
 
            // Take the second prime number
            second = primes[j];
 
            // Subtract the two prime numbers
            // from the N to obtain the third number
            third = num - first - second;
 
            // If the third number is prime
            if (binary_search(primes.begin(),
                              primes.end(), third)) {
                ans = true;
                break;
            }
        }
        if (ans)
            break;
    }
    // Print the three prime numbers
    // if the solution exists
    if (ans)
        cout << first << " "
             << second << " " << third << endl;
    else
        cout << -1 << endl;
}
 
// Driver code
int main()
{
    int n = 101;
 
    // Function for generating prime numbers
    // using Sieve of Eratosthenes
    initialize();
 
    // Function to print the three prime
    // numbers whose sum is equal to N
    findNums(n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
 
static int MAX = 100001;
   
// The vector primes holds
// the prime numbers
static ArrayList primes;
   
// Function to generate prime numbers
static void initialize()
{
     
    // Initialize the array elements to 0s
    boolean[] numbers = new boolean[MAX + 1];
    int n = MAX;
     
    for(int i = 2; i * i <= n; i++)
        if (!numbers[i])
            for(int j = i * i; j <= n; j += i)
             
                // Set the non-primes to true
                numbers[j] = true;
   
    // Fill the vector primes with prime
    // numbers which are marked as false
    // in the numbers array
    for(int i = 2; i <= n; i++)
        if (numbers[i] == false)
            primes.add(i);
}
   
// Function to print three prime numbers
// which sum up to the number N
static void findNums(int num)
{
    boolean ans = false;
    int first = -1, second = -1, third = -1;
     
    for(int i = 0; i < num; i++)
    {
         
        // Take the first prime number
        first = primes.get(i);
         
        for(int j = 0; j < num; j++)
        {
             
            // Take the second prime number
            second = primes.get(j);
   
            // Subtract the two prime numbers
            // from the N to obtain the third number
            third = num - first - second;
   
            // If the third number is prime
            if (Collections.binarySearch(
                primes, third) >= 0)
            {
                ans = true;
                break;
            }
        }
        if (ans)
            break;
    }
     
    // Print the three prime numbers
    // if the solution exists
    if (ans)
       System.out.println(first + " " +
                          second + " " +
                          third);
    else
        System.out.println(-1 );
}
 
// Driver code
public static void main (String[] args)
{
    int n = 101;
    primes = new ArrayList<>();
     
    // Function for generating prime numbers
    // using Sieve of Eratosthenes
    initialize();
     
    // Function to print the three prime
    // numbers whose sum is equal to N
    findNums(n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation of the approach
from math import sqrt
 
MAX = 100001;
 
# The vector primes holds
# the prime numbers
primes = [];
 
# Function to generate prime numbers
def initialize() :
 
    # Initialize the array elements to 0s
    numbers = [0]*(MAX + 1);
    n = MAX;
    for i in range(2, int(sqrt(n)) + 1) :
        if (not numbers[i]) :
            for j in range( i * i , n + 1, i) :
 
                # Set the non-primes to true
                numbers[j] = True;
 
    # Fill the vector primes with prime
    # numbers which are marked as false
    # in the numbers array
    for i in range(2, n + 1) :
        if (numbers[i] == False) :
            primes.append(i);
 
# Function to print three prime numbers
# which sum up to the number N
def findNums(num) :
 
    ans = False;
    first = -1;
    second = -1;
    third = -1;
    for i in range(num) :
 
        # Take the first prime number
        first = primes[i];
        for j in range(num) :
 
            # Take the second prime number
            second = primes[j];
 
            # Subtract the two prime numbers
            # from the N to obtain the third number
            third = num - first - second;
 
            # If the third number is prime
            if (third in primes) :
                ans = True;
                break;
     
        if (ans) :
            break;
     
    # Print the three prime numbers
    # if the solution exists
    if (ans) :
        print(first , second , third);
    else :
        print(-1);
 
# Driver code
if __name__ == "__main__" :
 
    n = 101;
 
    # Function for generating prime numbers
    # using Sieve of Eratosthenes
    initialize();
 
    # Function to print the three prime
    # numbers whose sum is equal to N
    findNums(n);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{  
  static int MAX = 100001;
 
  // The vector primes holds
  // the prime numbers
  static List primes = new List();
 
  // Function to generate prime numbers
  static void initialize()
  {
 
    // Initialize the array elements to 0s
    bool[] numbers = new bool[MAX + 1];
    int n = MAX;
    for (int i = 2; i * i <= n; i++)
      if (!numbers[i])
        for (int j = i * i; j <= n; j += i)
 
          // Set the non-primes to true
          numbers[j] = true;
 
    // Fill the vector primes with prime
    // numbers which are marked as false
    // in the numbers array
    for (int i = 2; i <= n; i++)
      if (numbers[i] == false)
        primes.Add(i);
  }
 
  // Function to print three prime numbers
  // which sum up to the number N
  static void findNums(int num)
  {
 
    bool ans = false;
    int first = -1, second = -1, third = -1;
    for (int i = 0; i < num; i++)
    {
 
      // Take the first prime number
      first = primes[i];
      for (int j = 0; j < num; j++)
      {
 
        // Take the second prime number
        second = primes[j];
 
        // Subtract the two prime numbers
        // from the N to obtain the third number
        third = num - first - second;
 
        // If the third number is prime
        if (Array.BinarySearch(primes.ToArray(), third) >= 0)
        {
          ans = true;
          break;
        }
      }
      if (ans)
        break;
    }
 
    // Print the three prime numbers
    // if the solution exists
    if (ans)
      Console.WriteLine(first + " " + second + " " + third);
    else
      Console.WriteLine(-1);
  }
 
  // Driver code
  static void Main()
  {
    int n = 101;
 
    // Function for generating prime numbers
    // using Sieve of Eratosthenes
    initialize();
 
    // Function to print the three prime
    // numbers whose sum is equal to N
    findNums(n);
  }
}
 
// This code is contributed by divyesh072019


输出:
2 2 97