📜  检查给定数字N是否为Moran数

📅  最后修改于: 2021-04-29 08:27:21             🧑  作者: Mango

给定整数N ,检查给定的数字是否为Moran数。 Moran数是Harshad数的子集。

例子:

方法:要解决上述问题,我们必须找到该数字的总和。然后通过将数字除以数字的总和来找到商,并检查商是否为质数,则给定的数字为Moran数。
下面是上述方法的实现:

C++
// C++ implementation to check if
// the number is Moran number
 
#include 
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


输出:
Yes