Open In App

Check whether given number N is a Moran Number or not

Last Updated : 29 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, check whether the given number is a Moran Number or not. Moran numbers are a subset of Harshad numbers.
 

A number N is a Moran number if N divided by the sum of its digits gives a prime number. For example some Moran numbers are 18, 21, 27, 42, 45 and so on.

Examples: 
 

Input: N = 34 
Output: No 
Explanation: 
34 is not a moran number because it is not completely divisible 7 (sum of its digits).
Input: N = 21 
Output: Yes 
Explanation: 
21 is a moran number because 21 divided by the sum of its digits gives a prime number. 
 

 

Approach: To solve the problem mentioned above we have to find the sum of digits of that number. Then find the quotient by dividing the number by the sum of its digits and check if the quotient is a prime then the given number is a Moran Number.
Below is the implementation of the above approach: 
 

C++




// C++ implementation to check if
// the number is Moran number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate digit sum
int digSum(int a)
{
    int sum = 0;
    while (a) {
        sum += a % 10;
        a = a / 10;
    }
    return sum;
}
 
// Function to check if number is prime
bool isPrime(int r)
{
    bool s = true;
 
    for (int i = 2; i * i <= r; i++) {
        if (r % i == 0) {
            s = false;
            break;
        }
    }
    return s;
}
 
// Function to check if
// number is moran number
void moranNo(int n)
{
    int dup = n;
 
    // Calculate digit sum
    int sum = digSum(dup);
 
    // Check if n is completely
    // divisible by digit sum
    if (n % sum == 0) {
 
        // Calculate the quotient
        int c = n / sum;
 
        // Check if the number is prime
        if (isPrime(c)) {
            cout << "Yes";
            return;
        }
    }
 
    cout << "No" << endl;
}
 
// Driver code
int main()
{
    int n = 21;
 
    moranNo(n);
 
    return 0;
}


Java




// Java implementation to check if
// the number is Moran number
import java.util.*;
import java.lang.*;
class GFG{
 
// Function to calculate digit sum
static int digSum(int a)
{
    int sum = 0;
    while (a != 0)
    {
        sum += a % 10;
        a = a / 10;
    }
    return sum;
}
 
// Function to check if number is prime
static boolean isPrime(int r)
{
    boolean s = true;
 
    for (int i = 2; i * i <= r; i++)
    {
        if (r % i == 0)
        {
            s = false;
            break;
        }
    }
    return s;
}
 
// Function to check if
// number is moran number
static void moranNo(int n)
{
    int dup = n;
 
    // Calculate digit sum
    int sum = digSum(dup);
 
    // Check if n is completely
    // divisible by digit sum
    if (n % sum == 0)
    {
 
        // Calculate the quotient
        int c = n / sum;
 
        // Check if the number is prime
        if (isPrime(c))
        {
            System.out.println("Yes");
            return;
        }
    }
    System.out.println("No");
}
 
// Driver code
public static void main(String[] args)
{
    int n = 21;
 
    moranNo(n);
}
}
 
// This code is contributed by offbeat


Python3




# Python3 implementation to check if
# the number is Moran number
 
# Function to calculate digit sum
def digSum(a):
 
    _sum = 0
 
    while (a):
        _sum += a % 10
        a = a // 10
 
    return _sum
 
# Function to check if number is prime
def isPrime(r):
 
    s = True
    i = 2
     
    while i * i <= r:
        if (r % i == 0):
            s = False
            break
        i += 1
     
    return s
 
# Function to check if
# number is moran number
def moranNo(n):
 
    dup = n
 
    # Calculate digit sum
    _sum = digSum(dup)
 
    # Check if n is completely
    # divisible by digit sum
    if (n % _sum == 0):
 
        # Calculate the quotient
        c = n // _sum
 
        # Check if the number is prime
        if (isPrime(c)):
            print("Yes")
            return
 
    print("No")
 
# Driver code
n = 21
 
moranNo(n)
 
# This code is contributed by divyamohan123


C#




// C# implementation to check if
// the number is Moran number
using System;
 
class GFG{
 
// Function to calculate digit sum
static int digSum(int a)
{
    int sum = 0;
    while (a != 0)
    {
        sum += a % 10;
        a = a / 10;
    }
    return sum;
}
 
// Function to check if number is prime
static bool isPrime(int r)
{
    bool s = true;
 
    for(int i = 2; i * i <= r; i++)
    {
       if (r % i == 0)
       {
           s = false;
           break;
       }
    }
    return s;
}
 
// Function to check if
// number is moran number
static void moranNo(int n)
{
    int dup = n;
 
    // Calculate digit sum
    int sum = digSum(dup);
 
    // Check if n is completely
    // divisible by digit sum
    if (n % sum == 0)
    {
 
        // Calculate the quotient
        int c = n / sum;
 
        // Check if the number is prime
        if (isPrime(c))
        {
            Console.Write("Yes");
            return;
        }
    }
    Console.Write("No");
}
 
// Driver code
public static void Main()
{
    int n = 21;
 
    moranNo(n);
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
 
// Javascript implementation to check if
// the number is Moran number
 
// Function to calculate digit sum
function digSum(a)
{
    let sum = 0;
    while (a) {
        sum += a % 10;
        a = Math.floor(a / 10);
    }
    return sum;
}
 
// Function to check if number is prime
function isPrime(r)
{
    let s = true;
 
    for (let i = 2; i * i <= r; i++) {
        if (r % i == 0) {
            s = false;
            break;
        }
    }
    return s;
}
 
// Function to check if
// number is moran number
function moranNo(n)
{
    let dup = n;
 
    // Calculate digit sum
    let sum = digSum(dup);
 
    // Check if n is completely
    // divisible by digit sum
    if (n % sum == 0) {
 
        // Calculate the quotient
        let c = n / sum;
 
        // Check if the number is prime
        if (isPrime(c)) {
            document.write("Yes");
            return;
        }
    }
 
    document.write("No" + "<br>");
}
 
// Driver code
 
    let n = 21;
 
    moranNo(n);
 
 
// This code is contributed by Mayank Tyagi
 
</script>


Output: 

Yes

 

Time complexity: O(sqrt(n))

Auxiliary Space: O(1)



Similar Reads

Print first K distinct Moran numbers from a given array
Given an array arr[] consisting of N distinct positive integers, the task is to print the first K distinct Moran Numbers from the given array. A number N is a Moran number if N divided by the sum of its digits gives a prime number. Examples: 18, 21, 27, 42, 45 Examples: Input: arr[] = {192, 21, 18, 138, 27, 42, 45}, K = 4 Output: 21, 27, 42, 45 Exp
11 min read
Javascript Program for Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Java Program for Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Python3 Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Php Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
2 min read
Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
12 min read
C++ Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
6 min read
Check whether the given number is Euclid Number or not
Given a positive integer n, the task is to check if it is Euclid Number or not. Print ‘YES’ if the given number is Euclid Number otherwise print ‘NO'. Euclid number : In Mathematics, Euclid numbers are integers of the form - [Tex]E_{n} = p_{n}\# + 1 [/Tex]where [Tex]p_{n}\# [/Tex]is product of first n prime numbers.The first few Euclid numbers are-
15 min read
Check whether a given number N is a Nude Number or not
Given an integer N, the task is to check whether N is a Nude number or not. A Nude number is a number that is divisible by all of its digits (which should be nonzero). Example: Input: N = 672 Output: Yes Explanation: Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output is Yes. Input: N = 450 Output: No Explanation: Si
4 min read
Check whether a given number is an ugly number or not
Given an integer N, the task is to find out whether the given number is an Ugly number or not . Ugly numbers are numbers whose only prime factors are 2, 3 or 5. Examples: Input: N = 14 Output: No Explanation: 14 is not ugly since it includes another prime factor 7. Input: N = 6 Output: Yes Explanation: 6 is a ugly since it includes 2 and 3. Approac
9 min read