Superperfect Number - GeeksforGeeks

28 Jan, 2022
Given an integer n. Check whether the number n is superperfect number or not. A superperfect number is a positive integer which satisfies σ2(n) = σ(σ(n)) = 2n, where σ is divisor summatory function.  
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)
 
The idea is simply straightforward. We just iterate from 1 to sqrt(n) and find sum of all divisors of n, lets we call this sum as n1. Now we again need to iterate from 1 to sqrt(n1) and find sum of all divisors. After that we just need to check whether the resulted sum is equal to 2*n or not.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to check whether number is// superperfect or not#include<bits/stdc++.h>using namespace std; // Function to calculate sum of all divisorsint 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 codeint main(){    int n = 16;    cout << (isSuperPerfect(n) ? "Yes\n" : "No\n");     n = 6;    cout << (isSuperPerfect(n) ? "Yes\n" : "No\n");    return 0;}
// 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 program to check whether number# is superperfect or notimport math # Function to calculate sum of all divisorsdef divSum(num):     # Final result of summation of divisors    result = 0     # find all divisors which divides 'num'    sq = int(math.sqrt(num))    for i in range(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 falsedef 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 coden = 16print ('Yes' if isSuperPerfect(n) else 'No') n = 6print ('Yes' if isSuperPerfect(n) else 'No')
// C# program to check whether number is// superperfect or notusing 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// PHP program to check whether// number is superperfect or not // Function to calculate// sum of all divisorsfunction divSum($num){         // Final result of    // summation of divisors    $result = 0;     // find all divisors    // which divides 'num'    for ($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.function isSuperPerfect($n){         // Find the sum of all    // divisors of number n    $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    $n = 16;    $hh = (isSuperPerfect($n) ? "Yes\n" : "No\n");        echo($hh);     $n = 6;    $hh=(isSuperPerfect($n) ? "Yes\n" : "No\n");        echo($hh); // This code is contributed by AJit?>
<script> // JavaScript program for the above approach      // Function to calculate sum of all divisors    function divSum(num)    {             // Final result of summation of divisors        let result = 0;                   // find all divisors which divides 'num'        for (let 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.    function isSuperPerfect(n)    {             // Find the sum of all divisors of number n        let 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         let n = 16;      document.write((isSuperPerfect(n) ? "Yes\n" : "No\n") + "<br />");                  n = 6;      document.write((isSuperPerfect(n) ? "Yes\n" : "No\n") + "<br />"); // This code is contributed by splevel62.</script>
Output:
Yes
No
Time complexity: O(sqrt(n + n1)) where n1 is sum of divisors of n. Auxiliary space: O(1)Facts about Supernumbers:  
If n is an even superperfect number, then n must be a power of 2 i.e., 2k such that 2k+1 – 1 is a Mersenne prime.It is not known whether there are any odd superperfect numbers. An odd superperfect number n would have to be a square number such that either n or σ(n) is divisible by at least three distinct primes. There are no odd superperfect numbers below 7×1024
If n is an even superperfect number, then n must be a power of 2 i.e., 2k such that 2k+1 – 1 is a Mersenne prime.
It is not known whether there are any odd superperfect numbers. An odd superperfect number n would have to be a square number such that either n or σ(n) is divisible by at least three distinct primes. There are no odd superperfect numbers below 7×1024
Reference: https://en.wikipedia.org/wiki/Superperfect_numberThis article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
splevel62
amartyaghoshgfg
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Program for Decimal to Binary Conversion
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Find all factors of a natural number | Set 1
Operators in C / C++