📜  乘以完美数

📅  最后修改于: 2021-04-29 17:29:40             🧑  作者: Mango

甲数N被认为是乘法完美如果N除法西格马(N),其中的数字西格马(N)= N个所有分频器的总和。
前几个乘数完美数是:

检查N是否为乘完美数

给定数字N ,任务是查找此数字是否为乘完美数。
例子:

方法:对于数N是乘-完全数,用以下条件应该成立:西格玛(N)%N = 0,其中西格马(N)= N个所有分频器的总和。因此,我们将找到N的所有除数之和,并检查它是否可被N整除。如果是可分割的,则打印“是”,否则,打印“否”
下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function to find the
// sum of divisors
int getSum(int n)
{
    int sum = 0;
 
    // Note that this loop
    // runs till square root of N
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
 
            // If divisors are equal,
            // take only one of them
            if (n / i == i)
                sum = sum + i;
 
            // Otherwise take both
            else {
                sum = sum + i;
                sum = sum + (n / i);
            }
        }
    }
 
    return sum;
}
 
// Function to check Multiply-perfect number
bool MultiplyPerfectNumber(int n)
{
    if (getSum(n) % n == 0)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
 
    int n = 28;
    if (MultiplyPerfectNumber(n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
     
// Function to find the
// sum of divisors
static int getSum(int n)
{
    int sum = 0;
 
    // Note that this loop
    // runs till square root of N
    for(int i = 1; i <= Math.sqrt(n); i++)
    {
       if (n % i == 0)
       {
            
           // If divisors are equal,
           // take only one of them
           if (n / i == i)
               sum = sum + i;
            
           // Otherwise take both
           else
           {
               sum = sum + i;
               sum = sum + (n / i);
           }
       }
    }
    return sum;
}
 
// Function to check Multiply-perfect number
static boolean MultiplyPerfectNumber(int n)
{
    if (getSum(n) % n == 0)
        return true;
    else
        return false;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 28;
     
    if (MultiplyPerfectNumber(n))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 implementation of the above approach
import math
 
# Function to find the
# sum of divisors
def getSum(n):
 
    sum1 = 0;
 
    # Note that this loop
    # runs till square root of N
    for i in range(1, int(math.sqrt(n))):
        if (n % i == 0):
 
            # If divisors are equal,
            # take only one of them
            if (n // i == i):
                sum1 = sum1 + i;
 
            # Otherwise take both
            else:
                sum1 = sum1 + i;
                sum1 = sum1 + (n // i);
             
    return sum1;
 
# Function to check Multiply-perfect number
def MultiplyPerfectNumber(n):
 
    if (getSum(n) % n == 0):
        return True;
    else:
        return False;
 
# Driver code
n = 28;
if (MultiplyPerfectNumber(n)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Code_Mech


C#
// C# implementation of the above approach
using System;
class GFG{
     
// Function to find the
// sum of divisors
static int getSum(int n)
{
    int sum = 0;
 
    // Note that this loop
    // runs till square root of N
    for(int i = 1; i <= Math.Sqrt(n); i++)
    {
       if (n % i == 0)
       {
            
           // If divisors are equal,
           // take only one of them
           if (n / i == i)
               sum = sum + i;
            
           // Otherwise take both
           else
           {
               sum = sum + i;
               sum = sum + (n / i);
           }
       }
    }
    return sum;
}
 
// Function to check Multiply-perfect number
static bool MultiplyPerfectNumber(int n)
{
    if (getSum(n) % n == 0)
        return true;
    else
        return false;
}
 
// Driver code
public static void Main()
{
    int n = 28;
     
    if (MultiplyPerfectNumber(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
Yes

参考: https : //en.wikipedia.org/wiki/Multiply_perfect_number