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)
We strongly recommend that you click here and practice it, before moving on to the solution.
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++
#include<bits/stdc++.h>
using namespace std;
int divSum( int num)
{
int result = 0;
for ( int i=1; i*i <= num; ++i)
{
if (num%i == 0)
{
if (i == (num/i))
result += i;
else
result += (i + num/i);
}
}
return result;
}
bool isSuperPerfect( int n)
{
int n1 = divSum(n);
return (2*n == divSum(n1));
}
int main()
{
int n = 16;
cout << (isSuperPerfect(n) ? "Yes\n" : "No\n" );
n = 6;
cout << (isSuperPerfect(n) ? "Yes\n" : "No\n" );
return 0;
}
|
Java
public class Divisors
{
static int divSum( int num)
{
int result = 0 ;
for ( int i= 1 ; i*i <= num; ++i)
{
if (num%i == 0 )
{
if (i == (num/i))
result += i;
else
result += (i + num/i);
}
}
return result;
}
static boolean isSuperPerfect( int n)
{
int n1 = divSum(n);
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" ));
}
}
|
Python3
import math
def divSum(num):
result = 0
sq = int (math.sqrt(num))
for i in range ( 1 , sq + 1 ):
if num % i = = 0 :
if i = = (num / / i):
result + = i
else :
result + = (i + num / / i)
return result
def isSuperPerfect(n):
n1 = divSum(n)
return divSum(n1) = = 2 * n
n = 16
print ( 'Yes' if isSuperPerfect(n) else 'No' )
n = 6
print ( 'Yes' if isSuperPerfect(n) else 'No' )
|
C#
using System;
class Divisors
{
static int divSum( int num)
{
int result = 0;
for ( int i = 1; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i == (num / i))
result += i;
else
result += (i + num / i);
}
}
return result;
}
static bool isSuperPerfect( int n)
{
int n1 = divSum(n);
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" ));
}
}
|
PHP
<?php
function divSum( $num )
{
$result = 0;
for ( $i = 1; $i * $i <= $num ; ++ $i )
{
if ( $num % $i == 0)
{
if ( $i == ( $num / $i ))
$result += $i ;
else
$result += ( $i + $num / $i );
}
}
return $result ;
}
function isSuperPerfect( $n )
{
$n1 = divSum( $n );
return (2 * $n == divSum( $n1 ));
}
$n = 16;
$hh = (isSuperPerfect( $n ) ? "Yes\n" : "No\n" );
echo ( $hh );
$n = 6;
$hh =(isSuperPerfect( $n ) ? "Yes\n" : "No\n" );
echo ( $hh );
?>
|
Javascript
<script>
function divSum(num)
{
let result = 0;
for (let i = 1; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i == (num/i))
result += i;
else
result += (i + num/i);
}
}
return result;
}
function isSuperPerfect(n)
{
let n1 = divSum(n);
return (2*n == divSum(n1));
}
let n = 16;
document.write((isSuperPerfect(n) ? "Yes\n" : "No\n" ) + "<br />" );
n = 6;
document.write((isSuperPerfect(n) ? "Yes\n" : "No\n" ) + "<br />" );
</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
Reference:
https://en.wikipedia.org/wiki/Superperfect_number
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...