Interprime

Dado un número n positivo, la tarea es verificar si el número dado es Interprime o no. Si el número dado es Interprime, escriba ‘SÍ’. De lo contrario, escriba ‘NO’.
Interprime : en matemáticas, un interprime es un número entero positivo que representa el promedio de dos números primos impares consecutivos.
Los primeros interprimos son: 
4, 6, 9, 12, 15, 18, 21, 26, 30, 34, 39, 42, 45, 50, 56, 60, 64, 69, 72, 76, 81, 86, 93, 99 
Ejemplos: 
 

Entrada: 12 
Salida: SÍ 
Explicación: 12 es el promedio de los números primos consecutivos 11 y 13.
Entrada: 20 
Salida: NO 

Una solución simple de este problema es generar números primos y verificar si podemos obtener el promedio dado de números primos impares consecutivos o no.
Acercarse : 
 

  1. Comience a generar el número primo ‘p’ de i=3 al número primo p > n
  2. si encontramos el promedio de p y p+1 como el número dado n, entonces deténgase e imprima ‘SÍ’ 
     
  3. Si no encontramos tal p y p+1 con el promedio dado, imprima ‘NO’.

Una solución eficiente es verificar el promedio de los números primos p1 y p2 únicamente, de modo que p1 y p2 sean consecutivos y p1 < n < p2.
Acercarse : 
 

  1. calcular los números primos consecutivos p1 y p2. tal que p1 < n < p2. 
     
  2. calcular el promedio de p1 y p2. 
     
  3. Imprimir ‘SÍ’ si obtenemos el promedio dado De lo contrario, imprimir ‘NO’.

C++

// CPP program to check if a number is
// interprime  or not
 
#include <bits/stdc++.h>
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

<?php
// PHP program to check if a
// number is interprime or not
 
// Function to check if a
// number is prime or not
function 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;
 
    for ($i = 5;
         $i * $i <= $n; $i = $i + 6)
    {
        if ($n % $i == 0 or
            $n % ($i + 2) == 0)
        {
            return false;
        }
    }
 
    return true;
}
 
// Function to check if the
// given number is interprime or not
function 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))
    {
        $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;
}
 
// Driver Code
$n = 9;
if (isInterprime($n))
    echo "YES";
else
    echo "NO";
     
// This code is contributed
// by Shashank
?>

Javascript

<script>
 
 
// Javascript program to check if a number is
// interprime  or not
 
// Function to check if a number is prime or not
function 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 || n % 3 == 0)
        return false;
 
    for (var 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
function isInterprime(n)
{
 
    // Smallest Interprime is 4
    // So the number less than 4
    // can not be a Interprime
 
    if (n < 4)
        return false;
 
    var prev_prime = n;
    var 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;
}
 
var n = 9;
if (isInterprime(n))
    document.write( "YES");
else
    document.write( "NO");
 
// This code is contributed by noob2000.
</script>

Producción: 
 

YES

Publicación traducida automáticamente

Artículo escrito por ihritik y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *