Open In App

Magnanimous Numbers

Last Updated : 14 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Magnanimous Number is a number of at least 2 digits such that the sum obtained inserting a “+” among its digit in any position gives a prime.

For example: 

4001 is Magnanimous Number because the numbers 4+001=5, 40+01=41 and 400+1=401 are all prime numbers.

Check if N is a Magnanimous number

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

Examples: 

Input: N = 4001 
Output: Yes 
Explanation: 
4+001=5, 40+01=41 and 400+1=401 are all prime numbers.

Input: N = 18 
Output: No

Approach: 

  1. Convert the number N to string
  2. Traverse the string and find all left part and right part of the string.
  3. Convert the left part and right part of the string to integer and check if the sum of left part and right part is not a prime number then return false
  4. Otherwise, return true at last

For example if N = 4001 
left part + right part = prime number 
4+001=5 = prime number 
40+01=41 prime number 
400+1=401 prime number

Below is the implementation of the above approach:

C++




// C++ implementation to check
// if a number is Magnanimous
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if n is prime
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 number is
// Magnanimous or not
bool isMagnanimous(int N)
{
    // converting the number to string
    string s = to_string(N);
 
    // finding length of string
    int l = s.length();
 
    // number should not be of single digit
    if (l < 2)
        return false;
 
    // loop to find all left and right
    // part of the string
    for (int i = 0; i < l - 1; i++) {
        string left = s.substr(0, i + 1);
        string right = s.substr(i + 1);
        int x = stoi(left);
        int y = stoi(right);
        if (!isPrime(x + y))
            return false;
    }
    return true;
}
 
// Driver Code
int main()
{
    int N = 12;
    isMagnanimous(N) ? cout << "Yes"
                     : cout << "No";
    return 0;
}


Java




// Java implementation to check 
// if a number is Magnanimous
class GFG{
 
// Function to check if n is prime
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 number is
// Magnanimous or not
static boolean isMagnanimous(int N)
{
     
    // Converting the number to string
    String s = Integer.toString(N);
 
    // Finding length of string
    int l = s.length();
 
    // Number should not be of single digit
    if (l < 2)
        return false;
 
    // Loop to find all left and right
    // part of the string
    for(int i = 0; i < l - 1; i++)
    {
        String left = s.substring(0, i + 1);
        String right = s.substring(i + 1);
        int x = Integer. valueOf(left);
        int y = Integer. valueOf(right);
         
        if (!isPrime(x + y))
            return false;
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 12;
     
    if(isMagnanimous(N))
        System.out.print("Yes\n");
    else
        System.out.print("No\n");
}
}
 
// This code is contributed by shubham


Python3




# Python3 implementation to check
# if a number is Magnanimous
 
# Function to check if n is prime
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 number is
# Magnanimous or not
def isMagnanimous(N):
 
    # Converting the number to string
    s = str(N)
 
    # Finding length of string
    l = len(s)
 
    # Number should not be of single digit
    if (l < 2):
        return False
 
    # Loop to find all left and right
    # part of the string
    for i in range(l - 1):
        left = s[0 : i + 1]
        right = s[i + 1 : ]
        x = int(left)
        y = int(right)
         
        if (not isPrime(x + y)):
            return False
 
    return True
 
# Driver code
N = 12
 
if isMagnanimous(N):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyeshrabadiya07


C#




// C# implementation to check
// if a number is Magnanimous
using System;
 
class GFG{
 
// Function to check if n is prime
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 number is
// Magnanimous or not
static bool isMagnanimous(int N)
{
     
    // Converting the number to string
    String s = N.ToString();
 
    // Finding length of string
    int l = s.Length;
 
    // Number should not be of single digit
    if (l < 2)
        return false;
 
    // Loop to find all left and right
    // part of the string
    for(int i = 0; i < l - 1; i++)
    {
        String left = s.Substring(0, i + 1);
        String right = s.Substring(i + 1);
         
        int x = int.Parse(left);
        int y = int. Parse(right);
         
        if (!isPrime(x + y))
            return false;
    }
    return true;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 12;
     
    if(isMagnanimous(N))
        Console.Write("Yes\n");
    else
        Console.Write("No\n");
}
}
 
// This code is contributed by amal kumar choubey


Javascript




<script>
    // Javascript implementation to check
    // if a number is Magnanimous
     
    // Function to check if n is prime
    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(let 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 number is
    // Magnanimous or not
    function isMagnanimous(N)
    {
 
        // Converting the number to string
        let s = N.toString();
 
        // Finding length of string
        let l = s.length;
 
        // Number should not be of single digit
        if (l < 2)
            return false;
 
        // Loop to find all left and right
        // part of the string
        for(let i = 0; i < l - 1; i++)
        {
            let left = s.substring(0, i + 1);
            let right = s.substring(i + 1);
            let x = parseInt(left);
            let y = parseInt(right);
 
            if (!isPrime(x + y))
                return false;
        }
        return true;
    }
     
    let N = 12;
      
    if(isMagnanimous(N))
        document.write("Yes");
    else
        document.write("No");
 
// This code is contributed by mukesh07.
</script>


Output: 

Yes

 

Time Complexity: O(n) 
Reference: http://www.numbersaplenty.com/set/magnanimous_number/
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads