📜  互质

📅  最后修改于: 2021-04-27 21:25:57             🧑  作者: Mango

给定正数n,任务是检查给定数是否为Interprime。如果给定的数字是Interprime,则打印“ YES”,否则打印“ NO”。

素数在数学中,素数是一个正整数,表示两个连续奇数质数的平均值。

前几个互质素是–
4, 6, 9, 12, 15, 18, 21, 26, 30, 34, 39, 42, 45, 50, 56, 60, 64, 69, 72, 76, 81, 86, 93, 99

例子:

这个问题的简单解决方案是生成质数,并检查是否可以从连续的奇数质数中获得给定的平均值。

方法 :

  1. 开始生成质数’p’形式,从i = 3到质数p> n
  2. 如果我们找到p和p + 1的平均值作为给定的数字n,则停止并打印’YES’
  3. 如果我们没有找到这样的p和p + 1,则给定的平均打印为“ NO”。

一种有效的解决方案是仅检查质数p1和p2的平均值,以使p1和p2连续且p1

方法 :

  1. 计算连续的质数p1和p2。使得p1
  2. 计算p1和p2的平均值。
  3. 如果得到给定的平均值,则打印“是”,否则,打印“否”。
C++
// CPP program to check if a number is
// interprime  or not
  
#include 
using namespace std;
  
// Function to check if a number is prime or not
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (int i = 5; i * i <= n; i = i + 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }
  
    return true;
}
  
// Function to check
// if the given number is interprime or not
bool isInterprime(int n)
{
  
    // Smallest Interprime is 4
    // So the number less than 4
    // can not be a Interprime
  
    if (n < 4)
        return false;
  
    int prev_prime = n;
    int next_prime = n;
  
    // Calculate first prime number < n
    while (!isPrime(prev_prime)) {
        prev_prime--;
    }
  
    // Calculate first prime number > n
    while (!isPrime(next_prime)) {
        next_prime++;
    }
  
    // Check if prev_prime and next_prime
    // have the same average
    if ((prev_prime + next_prime) == 2 * n)
        return true;
    else
        return false;
}
  
int main()
{
    int n = 9;
    if (isInterprime(n))
        cout << "YES";
    else
        cout << "NO";
  
    return 0;
}


Java
// JAVA program to check if a number is
// interprime  or not
  
class GFG {
    // Function to check if a number is prime or not
    static boolean isPrime(int n)
    {
        // Corner cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
  
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
  
        for (int i = 5; i * i <= n; i = i + 6) {
            if (n % i == 0 || n % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }
  
    // Function to check
    // if the given number is interprime or not
    static boolean isInterprime(int n)
    {
  
        // Smallest Interprime is 4
        // So the number less than 4
        // can not be a Interprime
  
        if (n < 4)
            return false;
  
        int prev_prime = n;
        int next_prime = n;
  
        // Calculate first prime number < n
        while (!isPrime(prev_prime)) {
            prev_prime--;
        }
  
        // Calculate first prime number > n
        while (!isPrime(next_prime)) {
            next_prime++;
        }
  
        // check if next_prime and prev_prime
        // have the same average
        if ((prev_prime + next_prime) == 2 * n)
            return true;
        else
            return false;
    }
  
    public static void main(String[] args)
    {
  
        int n = 9;
        if (isInterprime(n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


Python3
# Python 3 program to check if a number is   
# interprime  or not  
      
# Utility function to check  
# if a number is prime or not  
def isPrime(n) :   
  
    # Corner cases   
    if (n <= 1) :   
        return False
    if (n <= 3) :   
        return True
      
    # This is checked so that we can skip   
    # middle five numbers in below loop   
    if (n % 2 == 0 or n % 3 == 0) :   
        return False
      
    i = 5
    while (i * i <= n) :   
        if (n % i == 0 or n % (i + 2) == 0) :   
            return False
        i = i + 6
      
    return True
  
# Function to check 
# if the given number is interprime or not 
def isInterprime( n):
  
      
     # Smallest Interprime is 4
     # So the number less than 4
     # can not be a Interprime
       
     if (n < 4):
        return False
       
       
     prev_prime = n
     next_prime = n
       
     # Calculate first prime number < n
     while (isPrime(prev_prime)== 0):
         prev_prime = prev_prime-1
      
       
     # Calculate first prime number > n
     while (isPrime(next_prime)== 0):
         next_prime = next_prime + 1
       
     # check if next_prime and prev_prime
     # have the same average
     if ((prev_prime + next_prime)== 2 * n):
         return True
     else:
        return False    
          
n = 9
if(isInterprime(n)):
    print("YES")
else:
    print("NO")


C#
// C# program to check if a number is
// interprime  or not
  
using System;
class GFG {
    // Function to check if a number is prime or not
    static bool isPrime(int n)
    {
        // Corner cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
  
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
  
        for (int i = 5; i * i <= n; i = i + 6) {
            if (n % i == 0 || n % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }
  
    // Function to check
    // if the given number is interprime or not
    static bool isInterprime(int n)
    {
  
        // Smallest Interprime is 4
        // So the number less than 4
        // can not be a Interprime
  
        if (n < 4)
            return false;
  
        int prev_prime = n;
        int next_prime = n;
  
        // Calculate first prime number < n
        while (!isPrime(prev_prime)) {
            prev_prime--;
        }
  
        // Calculate first prime number > n
        while (!isPrime(next_prime)) {
            next_prime++;
        }
  
        // check if next_prime and prev_prime
        // have the same average
        if ((prev_prime + next_prime) == 2 * n)
            return true;
        else
            return false;
    }
  
    public static void Main()
    {
  
        int n = 9;
        if (isInterprime(n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}


PHP
 n
    while (!isPrime($next_prime)) 
    {
        $next_prime++;
    }
  
    // Check if prev_prime and 
    // next_prime have the same average
    if (($prev_prime + 
         $next_prime) == 2 * $n)
        return true;
    else
        return false;
}
  
// Driver Code
$n = 9;
if (isInterprime($n))
    echo "YES";
else
    echo "NO";
      
// This code is contributed
// by Shashank
?>


输出:

YES