Given a number, determine whether it is a valid Hyperperfect Number.
A number n is called k-hyperperfect if: n = 1 + k ?idi where all di are the proper divisors of n.
Taking k = 1 will give us perfect numbers.
The first few k-hyperperfect numbers are 6, 21, 28, 301, 325, 496, 697, … with the corresponding values of k being 1, 2, 1, 6, 3, 1, 12, …
Examples:
Input : N = 36, K = 1
Output : 34 is not 1-HyperPerfect
Explanation:
The Divisors of 36 are 2, 3, 4, 6, 9, 12, 18
the sum of the divisors is 54.
For N = 36 to be 1-Hyperperfect, it would
require 36 = 1 + 1(54), which we see, is
invalid
Input : N = 325, K = 3
Output : 325 is 3-HyperPerfect
Explanation:
We can use the first condition to evaluate this
as K is odd and > 1 so here p = (3*k+1)/2 = 5,
q = (3*k+4) = 13 p and q are both prime, so we
compute p^2 * q = 5 ^ 2 * 13 = 325
Hence N is a valid HyperPerfect number
C++
#include <bits/stdc++.h>
using namespace std;
int divisorSum( int N, int K)
{
int sum = 0;
for ( int i = 2 ; i <= ceil ( sqrt (N)) ; i++)
if (N % i == 0)
sum += ( i + N/i );
return sum;
}
bool isPrime( int n)
{
if (n == 1 || n == 0)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i=5; i*i<=n; i=i+6)
if (n % i == 0 || n % ( i + 2 ) == 0)
return false ;
return true ;
}
bool isHyperPerfect( int N, int K)
{
int sum = divisorSum(N, K);
if ((1 + K * (sum)) == N)
return true ;
else
return false ;
}
int main()
{
int N1 = 1570153, K1 = 12;
int N2 = 321, K2 = 3;
if (isHyperPerfect(N1, K1))
cout << N1 << " is " << K1
<< "-HyperPerfect" << "\n" ;
else
cout << N1 << " is not " << K1
<< "-HyperPerfect" << "\n" ;
if (isHyperPerfect(N2, K2))
cout << N2 << " is " << K2
<< "-HyperPerfect" << "\n" ;
else
cout << N2 << " is not " << K2
<< "-HyperPerfect" << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int divisorSum( int N,
int K)
{
int sum = 0 ;
for ( int i = 2 ;
i <= Math.ceil(Math.sqrt(N));
i++)
if (N % i == 0 )
sum += (i + N / i);
return sum;
}
static boolean isPrime( int n)
{
if (n == 1 || n == 0 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 ||
n % 3 == 0 )
return false ;
for ( int i = 5 ;
i * i <= n; i = i + 6 )
if (n % i == 0 ||
n % ( i + 2 ) == 0 )
return false ;
return true ;
}
static boolean isHyperPerfect( int N,
int K)
{
int sum = divisorSum(N, K);
if (( 1 + K * (sum)) == N)
return true ;
else
return false ;
}
public static void main (String[] args)
{
int N1 = 1570153 , K1 = 12 ;
int N2 = 321 , K2 = 3 ;
if (isHyperPerfect(N1, K1))
System.out.println (N1 + " is " + K1 +
"-HyperPerfect" );
else
System.out.println(N1 + " is not " + K1 +
"-HyperPerfect" );
if (isHyperPerfect(N2, K2))
System.out.println( N2 + " is " + K2 +
"-HyperPerfect" );
else
System.out.println(N2 + " is not " + K2 +
"-HyperPerfect" );
}
}
|
Python3
import math
def divisorSum(N, K):
Sum = 0
for i in range ( 2 , math.ceil(math.sqrt(N))):
if (N % i = = 0 ):
Sum + = (i + int (N / i))
return Sum
def isPrime(n):
if (n = = 1 or n = = 0 ):
return False
if (n < = 3 ):
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
i = 5
while (i * i < = n):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
i + = 6
return True
def isHyperPerfect(N, K):
Sum = divisorSum(N, K)
if (( 1 + K * ( Sum )) = = N):
return True
else :
return False
N1 = 1570153
K1 = 12
N2 = 321
K2 = 3
if (isHyperPerfect(N1, K1)):
print (N1, " is " , K1,
"-HyperPerfect" , sep = "")
else :
print (N1, " is not " , K1,
"-HyperPerfect" , sep = "")
if (isHyperPerfect(N2, K2)):
print (N2, " is " , K2,
"-HyperPerfect" , sep = "")
else :
print (N2, " is not " , K2,
"-HyperPerfect" , sep = "")
|
C#
using System;
class GFG
{
static int divisorSum( int N,
int K)
{
int sum = 0;
for ( int i = 2 ;
i <= Math.Ceiling(Math.Sqrt(N));
i++)
if (N % i == 0)
sum += (i + N / i);
return sum;
}
static bool isPrime( int n)
{
if (n == 1 || n == 0)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 ||
n % 3 == 0)
return false ;
for ( int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % ( i + 2 ) == 0)
return false ;
return true ;
}
static bool isHyperPerfect( int N,
int K)
{
int sum = divisorSum(N, K);
if ((1 + K * (sum)) == N)
return true ;
else
return false ;
}
static public void Main ()
{
int N1 = 1570153, K1 = 12;
int N2 = 321, K2 = 3;
if (isHyperPerfect(N1, K1))
Console.WriteLine(N1 + " is " + K1 +
"-HyperPerfect" );
else
Console.WriteLine(N1 + " is not " + K1 +
"-HyperPerfect" );
if (isHyperPerfect(N2, K2))
Console.WriteLine( N2 + " is " + K2 +
"-HyperPerfect" );
else
Console.WriteLine(N2 + " is not " + K2 +
"-HyperPerfect" );
}
}
|
PHP
<?php
function divisorSum( $N , $K )
{
$sum = 0;
for ( $i = 2 ;
$i <= ceil (sqrt( $N )) ; $i ++)
if ( $N % $i == 0)
$sum += ( $i + $N / $i );
return $sum ;
}
function isPrime( $n )
{
if ( $n == 1 || $n == 0)
return false;
if ( $n <= 3)
return true;
if ( $n % 2 == 0 || $n % 3 == 0)
return false;
for ( $i = 5;
$i * $i <= $n ; $i = $i + 6)
if ( $n % $i == 0 ||
$n % ( $i + 2) == 0)
return false;
return true;
}
function isHyperPerfect( $N , $K )
{
$sum = divisorSum( $N , $K );
if ((1 + $K * ( $sum )) == $N )
return true;
else
return false;
}
$N1 = 1570153;
$K1 = 12;
$N2 = 321;
$K2 = 3;
if (isHyperPerfect( $N1 , $K1 ))
echo $N1 , " is " , $K1 ,
"-HyperPerfect" , "\n" ;
else
echo $N1 , " is not " , $K1 ,
"-HyperPerfect" , "\n" ;
if (isHyperPerfect( $N2 , $K2 ))
echo $N2 , " is " , K2,
"-HyperPerfect" , "\n" ;
else
echo $N2 , " is not " , $K2 ,
"-HyperPerfect" , "\n" ;
?>
|
Javascript
<script>
function divisorSum(N, K)
{
let sum = 0;
for (let i = 2;
i <= Math.ceil(Math.sqrt(N));
i++)
if (N % i == 0)
sum += (i + parseInt(N / i, 10));
return sum;
}
function isPrime(n)
{
if (n == 1 || n == 0)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function isHyperPerfect(N, K)
{
let sum = divisorSum(N, K);
if ((1 + K * (sum)) == N)
return true ;
else
return false ;
}
let N1 = 1570153, K1 = 12;
let N2 = 321, K2 = 3;
if (isHyperPerfect(N1, K1))
document.write(N1 + " is " + K1 +
"-HyperPerfect" + "</br>" );
else
document.write(N1 + " is not " + K1 +
"-HyperPerfect" + "</br>" );
if (isHyperPerfect(N2, K2))
document.write(N2 + " is " + K2 +
"-HyperPerfect" + "</br>" );
else
document.write(N2 + " is not " + K2 +
"-HyperPerfect" + "</br>" );
</script>
|
Output:
1570153 is 12-HyperPerfect
321 is not 3-HyperPerfect
Time Complexity: O(?n)
Auxiliary Space: O(1)
Given k, we can perform a few checks in special cases to determine whether the number is hyperperfect:
- If K > 1 and K is odd , then let p = (3*k+1)/2 and q = 3*k+4 . If p and q are prime, then p2q is k-hyperperfect
- If p and q are distinct odd primes such that K(p + q ) = pq – 1 for some positive integral value of K, then pq is k-hyperperfect
Reference :
https://en.wikipedia.org/wiki/Hyperperfect_number
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Jun, 2022
Like Article
Save Article