Skip to content
Related Articles

Related Articles

Improve Article

Additive Prime Number

  • Last Updated : 06 May, 2021

Given a number N, the task is to check if N is an Additive Prime Number or not. If N is an Additive Prime Number then print “Yes” else print “No”.

An Additive Prime Number is a prime number P such that the sum of digits of P is also a prime number
For example, 23 is an Additive prime because 2 + 3 = 5 which is a prime number. 
 

Examples:  

Input: N = 23 
Output: Yes 
Explanation: 
Sum of digits of 23 = 2 + 3 = 5.

Input: N = 10 
Output: No 
 



Approach: The idea is to find the sum of digits of the number N and check if it is prime or not. If sum is a prime number then print “Yes” else print “No”.

Below is the implementation of the above approach: 

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>
Output: 
Yes

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :