甲数N被认为是乘法完美如果N除法西格马(N),其中的数字西格马(N)= N个所有分频器的总和。
前几个乘数完美数是:
1, 6, 28, 120, 496, 672, ……..
检查N是否为乘完美数
给定数字N ,任务是查找此数字是否为乘完美数。
例子:
Input: N = 120
Output: YES
Explanation:
Sum of 120’s divisors is 1 + 2 + 3 + 4 + 5 + 6 + 8 + 10 + 12 + 15 + 20 + 24 + 30 + 40 + 60 + 120 = 360 and 120 divides 360.
Therefore, 120 is a Multiply-perfect number.
Input: N = 32
Output: No
方法:对于数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