📜  超完美数

📅  最后修改于: 2021-04-24 15:47:23             🧑  作者: Mango

给定整数n。检查数字n是否为完美数字。甲超完全数是一个正整数,它满足σ2(N)=σ(σ(n))的= 2,其中σ是除数summatory函数。

Input: n = 16
Output: yes
Explanation:
16 is a superperfect number as σ(16) = 1 + 2 + 4 + 8 + 16 = 31,
and σ(31) = 1 + 31 = 32, 
thus σ(σ(16)) = 32 = 2 × 16.

Input: n = 8
Output: no 
Explanation:
σ(8) = 1 + 2 + 4 + 8 = 15
and σ(15) = 1 + 3 + 5 + 15 = 24
thus ( σ(σ(8)) = 24 ) ≠ (2 * 8 = 26)
我们强烈建议您单击此处并进行实践,然后再继续解决方案。

这个想法很简单。我们只是从1迭代到sqrt(n)并找到n的所有除数的总和,我们将此总和称为n1。现在我们再次需要从1迭代到sqrt(n1)并找到所有除数的和。之后,我们只需要检查结果和是否等于2 * n。

C++
// C++ program to check whether number is
// superperfect or not
#include
using namespace std;
 
// Function to calculate sum of all divisors
int divSum(int num)
{
    // Final result of summation of divisors
    int result = 0;
 
    // find all divisors which divides 'num'
    for (int i=1; i*i <= num; ++i)
    {
        // if 'i' is divisor of 'num'
        if (num%i == 0)
        {
            // if both divisors are same then add
            // it only once else add both
            if (i == (num/i))
                result += i;
            else
                result += (i + num/i);
        }
    }
 
    return result;
}
 
// Returns true if n is Super Perfect else false.
bool isSuperPerfect(int n)
{
    // Find the sum of all divisors of number n
    int n1 = divSum(n);
 
    // Again find the sum of all divisors of n1
    // and check if sum is equal to n1
    return (2*n == divSum(n1));
}
 
//Driver code
int main()
{
    int n = 16;
    cout << (isSuperPerfect(n) ? "Yes\n" : "No\n");
 
    n = 6;
    cout << (isSuperPerfect(n) ? "Yes\n" : "No\n");
    return 0;
}


Java
// Java program to check whether number is
// superperfect or not
 
public class Divisors
{
    // Function to calculate sum of all divisors
    static int divSum(int num)
    {
        // Final result of summation of divisors
        int result = 0;
         
        // find all divisors which divides 'num'
        for (int i=1; i*i <= num; ++i)
        {
            // if 'i' is divisor of 'num'
            if (num%i == 0)
            {
                // if both divisors are same then add
                // it only once else add both
                if (i == (num/i))
                    result += i;
                else
                    result += (i + num/i);
            }
        }
        return result;
    }
     
    // Returns true if n is Super Perfect else false.
    static boolean isSuperPerfect(int n)
    {
        // Find the sum of all divisors of number n
        int n1 = divSum(n);
        // Again find the sum of all divisors of n1
        // and check if sum is equal to n1
        return (2*n == divSum(n1));
    }
     
    public static void main (String[] args)
    {
        int n = 16;
        System.out.printf((isSuperPerfect(n) ? "Yes\n" : "No\n"));
         
        n = 6;
        System.out.printf((isSuperPerfect(n) ? "Yes\n" : "No\n"));
    }
}
 
// This code is contributed by Saket Kumar


Python
# Python program to check whether number
# is superperfect or not
import math
 
# Function to calculate sum of all divisors
def divSum(num):
 
    # Final result of summation of divisors
    result = 0
 
    # find all divisors which divides 'num'
    sq = int(math.sqrt(num))
    for i in xrange(1, sq+1):
 
        # if 'i' is divisor of 'num'
        if num %i == 0:
 
            # if both divisors are same then add
            # it only once else add both
            if i == (num/i):
                result += i
            else:
                result += (i + num/i)
 
 
 
    return result
 
# Returns true if n is superperfect else false
def isSuperPerfect(n):
 
    # Find the sum of all divisors of number n
    n1 = divSum(n)
 
    # Again find the sum of all divisors of n1
    return divSum(n1) == 2*n
 
 
#Driver code
n = 16
print 'Yes' if isSuperPerfect(n) else 'No'
 
n = 6
print 'Yes' if isSuperPerfect(n) else 'No'


C#
// C# program to check whether number is
// superperfect or not
using System;
 
class Divisors
{
    // Function to calculate sum of all divisors
    static int divSum(int num)
    {
        // Final result of summation of divisors
        int result = 0;
         
        // find all divisors which divides 'num'
        for (int i = 1; i * i <= num; ++i)
        {
            // if 'i' is divisor of 'num'
            if (num % i == 0)
            {
                // if both divisors are same then add
                // it only once else add both
                if (i == (num / i))
                    result += i;
                else
                    result += (i + num / i);
            }
        }
        return result;
    }
     
    // Returns true if n is Super Perfect else false.
    static bool isSuperPerfect(int n)
    {
        // Find the sum of all divisors of number n
        int n1 = divSum(n);
        // Again find the sum of all divisors of n1
        // and check if sum is equal to n1
        return (2 * n == divSum(n1));
    }
     
    public static void Main ()
    {
        int n = 16;
        Console.WriteLine((isSuperPerfect(n) ? "Yes" : "No"));
         
        n = 6;
        Console.WriteLine((isSuperPerfect(n) ? "Yes" : "No"));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


Output:
Yes
No

时间复杂度: O(sqrt(n + n1))其中n1是n的除数之和。
辅助空间: O(1)
关于超数的事实:

  1. 如果n是一个偶数,那么n必须是2的幂,即2 k,这样2 k + 1 – 1是梅森素数。
  2. 不知道是否有任何奇数个超完美数。奇数个完美的数n必须是一个平方数,以使n或σ(n)被至少三个不同的素数整除。 7×10 24以下没有奇数个完美的数