给定正数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
例子:
Input : 12
Output : YES
Explanation: 12 is average of consecutive prime number 11 and 13.
Input : 20
Output : NO
这个问题的简单解决方案是生成质数,并检查是否可以从连续的奇数质数中获得给定的平均值。
方法 :
- 开始生成质数’p’形式,从i = 3到质数p> n
- 如果我们找到p和p + 1的平均值作为给定的数字n,则停止并打印’YES’
- 如果我们没有找到这样的p和p + 1,则给定的平均打印为“ NO”。
一种有效的解决方案是仅检查质数p1和p2的平均值,以使p1和p2连续且p1 方法 : 输出:
C++
// CPP program to check if a number is
// interprime or not
#include
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