Número primo aditivo

Dado un número N , la tarea es verificar si N es un número primo aditivo o no. Si N es un número primo aditivo , escriba «Sí» , de lo contrario, escriba «No» .

Un número primo aditivo es un número primo P tal que la suma de los dígitos de P también es un número primo
Por ejemplo, 23 es un número primo aditivo porque 2 + 3 = 5, que es un número primo. 
 

Ejemplos:  

Entrada: N = 23 
Salida: Sí 
Explicación: 
Suma de dígitos de 23 = 2 + 3 = 5.

Entrada: N = 10 
Salida: No 
 

Planteamiento: La idea es encontrar la suma de los dígitos del número N y comprobar si es primo o no. Si la suma es un número primo, imprima «Sí» , de lo contrario, imprima «No» .

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Check if N is prime or not
bool isPrime(int n)
{
    // Corner Cases
    if (n <= 1)
        return false;
 
    if (n <= 3)
        return true;
 
    // This is checked to skip
    // middle five numbers
    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 get sum of digits
int getSum(int n)
{
    int sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = n / 10;
    }
 
    // Return the sum of digits
    return sum;
}
 
// Function to check whether
// the given number is
// Additive Prime number or not
bool isAdditivePrime(int n)
{
    // If number is not prime
    if (!isPrime(n))
        return false;
 
    // Check if sum of digits
    // is prime or not
    return isPrime(getSum(n));
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 23;
 
    // Function Call
    if (isAdditivePrime(N))
        cout << "Yes";
    else
        cout << "No";
}

Java

// Java program for the above approach
class GFG{
 
// Check if N is prime or not
static boolean isPrime(int n)
{
     
    // Corner Cases
    if (n <= 1)
        return false;
 
    if (n <= 3)
        return true;
 
    // This is checked to skip
    // middle five numbers
    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 get sum of digits
static int getSum(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
 
    // Return the sum of digits
    return sum;
}
 
// Function to check whether
// the given number is
// Additive Prime number or not
static boolean isAdditivePrime(int n)
{
     
    // If number is not prime
    if (!isPrime(n))
        return false;
 
    // Check if sum of digits
    // is prime or not
    return isPrime(getSum(n));
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Number N
    int n = 23;
 
    // Function Call
    if (isAdditivePrime(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Pratima Pandey

Python3

# Python3 program for the above approach
 
# Check if N is prime or not
def isPrime(n):
 
    # Corner Cases
    if (n <= 1):
        return False
 
    if (n <= 3):
        return True
 
    # This is checked to skip
    # middle five numbers
    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 get sum of digits
def getSum(n):
 
    sum = 0
    while (n != 0):
        sum = sum + n % 10
        n = n / 10
 
    # Return the sum of digits
    return sum
 
# Function to check whether
# the given number is
# Additive Prime number or not
def isAdditivePrime(n):
 
    # If number is not prime
    if (not isPrime(n)):
        return False
 
    # Check if sum of digits
    # is prime or not
    return isPrime(getSum(n))
 
# Driver Code
 
# Given Number N
N = 23
 
# Function Call
if (isAdditivePrime(N)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by Pratik Basu

C#

// C# program for the above approach
using System;
class GFG{
 
// Check if N is prime or not
static bool isPrime(int n)
{
     
    // Corner Cases
    if (n <= 1)
        return false;
 
    if (n <= 3)
        return true;
 
    // This is checked to skip
    // middle five numbers
    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 get sum of digits
static int getSum(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
 
    // Return the sum of digits
    return sum;
}
 
// Function to check whether
// the given number is
// Additive Prime number or not
static bool isAdditivePrime(int n)
{
     
    // If number is not prime
    if (!isPrime(n))
        return false;
 
    // Check if sum of digits
    // is prime or not
    return isPrime(getSum(n));
}
 
// Driver code
public static void Main()
{
     
    // Given Number N
    int n = 23;
 
    // Function Call
    if (isAdditivePrime(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Code_Mech

Javascript

<script>
 
// JavaScript program for the above approach
 
// Check if N is prime or not
function isPrime(n)
{
     
    // Corner Cases
    if (n <= 1)
        return false;
 
    if (n <= 3)
        return true;
 
    // This is checked to skip
    // middle five numbers
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for(let i = 5; i * i <= n; i = i + 6)
       if (n % i == 0 || n % (i + 2) == 0)
           return false;
 
    return true;
}
 
// Function to get sum of digits
function getSum(n)
{
    let sum = 0;
     
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
 
    // Return the sum of digits
    return sum;
}
 
// Function to check whether
// the given number is
// Additive Prime number or not
function isAdditivePrime(n)
{
     
    // If number is not prime
    if (!isPrime(n))
        return false;
 
    // Check if sum of digits
    // is prime or not
    return isPrime(getSum(n));
}
 
// Driver Code
 
// Given Number N
let n = 23;
 
// Function Call
if (isAdditivePrime(n))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by susmitakundugoaldanga
 
</script>
Producción: 

Yes

 

Publicación traducida automáticamente

Artículo escrito por spp____ 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 *