📌  相关文章
📜  找到第三个数字,使所有三个数字的和成为质数

📅  最后修改于: 2021-04-24 15:58:49             🧑  作者: Mango

给定两个数字AB。任务是找到大于或等于1的最小正整数,以使所有三个数字的和成为质数。
例子:

方法:

  1. 首先,将给定2个数字的存储在sum变量中。
  2. 现在,检查sum和1的按位与(&)是否等于1(检查数字是否为奇数)。
  3. 如果等于1,则将2分配给变量temp,然后转到步骤4。
  4. 否则检查sum和temp变量的和值是否为质数。如果是质数,则打印temp变量的值,否则将2添加到temp变量,直到它小于质数。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function that will check
// whether number is prime or not
bool prime(int n)
{
    for (int i = 2; i * i <= n; i++) {
 
        if (n % i == 0)
            return false;
    }
    return true;
}
 
// Function to print the 3rd number
void thirdNumber(int a, int b)
{
    int sum = 0, temp = 0;
 
    sum = a + b;
    temp = 1;
 
    // If the sum is odd
    if (sum & 1) {
        temp = 2;
    }
 
    // If sum is not prime
    while (!prime(sum + temp)) {
        temp += 2;
    }
 
    cout << temp;
}
 
// Driver code
int main()
{
    int a = 3, b = 5;
 
    thirdNumber(a, b);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
     
class GFG
{
     
// Function that will check
// whether number is prime or not
static boolean prime(int n)
{
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
 
// Function to print the 3rd number
static void thirdNumber(int a, int b)
{
    int sum = 0, temp = 0;
 
    sum = a + b;
    temp = 1;
 
    // If the sum is odd
    if (sum == 0)
    {
        temp = 2;
    }
 
    // If sum is not prime
    while (!prime(sum + temp))
    {
        temp += 2;
    }
    System.out.print(temp);
}
 
// Driver code
static public void main (String []arr)
{
    int a = 3, b = 5;
     
    thirdNumber(a, b);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the above approach
 
# Function that will check
# whether number is prime or not
def prime(n):
    for i in range(2, n + 1):
        if i * i > n + 1:
            break
 
        if (n % i == 0):
            return False
 
    return True
 
# Function to print the 3rd number
def thirdNumber(a, b):
    summ = 0
    temp = 0
 
    summ = a + b
    temp = 1
 
    #If the summ is odd
    if (summ & 1):
        temp = 2
 
    #If summ is not prime
    while (prime(summ + temp) == False):
        temp += 2
 
    print(temp)
 
# Driver code
a = 3
b = 5
 
thirdNumber(a, b)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
     
// Function that will check
// whether number is prime or not
static bool prime(int n)
{
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
 
// Function to print the 3rd number
static void thirdNumber(int a, int b)
{
    int sum = 0, temp = 0;
 
    sum = a + b;
    temp = 1;
 
    // If the sum is odd
    if (sum == 0)
    {
        temp = 2;
    }
 
    // If sum is not prime
    while (!prime(sum + temp))
    {
        temp += 2;
    }
    Console.Write (temp);
}
 
// Driver code
static public void Main ()
{
    int a = 3, b = 5;
     
    thirdNumber(a, b);
}
}
 
// This code is contributed by Sachin.


Javascript


输出:
3