Determine whether a given number is a Hyperperfect Number
Last Updated :
23 Jun, 2022
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++
// C++ 4.3.2 program to check whether a
// given number is k-hyperperfect
#include <bits/stdc++.h>
using namespace std;
// function to find the sum of all
// proper divisors (excluding 1 and N)
int divisorSum(int N, int K)
{
int sum = 0;
// Iterate only until sqrt N as we are
// going to generate pairs to produce
// divisors
for (int i = 2 ; i <= ceil(sqrt(N)) ; i++)
// As divisors occur in pairs, we can
// take the values i and N/i as long
// as i divides N
if (N % i == 0)
sum += ( i + N/i );
return sum;
}
// Function to check whether the given number
// is prime
bool isPrime(int n)
{
//base and corner cases
if (n == 1 || n == 0)
return false;
if (n <= 3)
return true;
// Since integers can be represented as
// some 6*k + y where y >= 0, we can eliminate
// all integers that can be expressed in this
// form
if (n % 2 == 0 || n % 3 == 0)
return false;
// start from 5 as this is the next prime number
for (int i=5; i*i<=n; i=i+6)
if (n % i == 0 || n % ( i + 2 ) == 0)
return false;
return true;
}
// Returns true if N is a K-Hyperperfect number
// Else returns false.
bool isHyperPerfect(int N, int K)
{
int sum = divisorSum(N, K);
// Condition from the definition of hyperperfect
if ((1 + K * (sum)) == N)
return true;
else
return false;
}
// Driver function to test for hyperperfect numbers
int main()
{
int N1 = 1570153, K1 = 12;
int N2 = 321, K2 = 3;
// First two statements test against the condition
// N = 1 + K*(sum(proper divisors))
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
// Java program to check
// whether a given number
// is k-hyperperfect
import java.io.*;
class GFG
{
// function to find the
// sum of all proper
// divisors (excluding
// 1 and N)
static int divisorSum(int N,
int K)
{
int sum = 0;
// Iterate only until
// sqrt N as we are
// going to generate
// pairs to produce
// divisors
for (int i = 2 ;
i <= Math.ceil(Math.sqrt(N));
i++)
// As divisors occur in
// pairs, we can take
// the values i and N/i
// as long as i divides N
if (N % i == 0)
sum += (i + N / i);
return sum;
}
// Function to check
// whether the given
// number is prime
static boolean isPrime(int n)
{
// base and corner cases
if (n == 1 || n == 0)
return false;
if (n <= 3)
return true;
// Since integers can be
// represented as some
// 6*k + y where y >= 0,
// we can eliminate all
// integers that can be
// expressed in this form
if (n % 2 == 0 ||
n % 3 == 0)
return false;
// start from 5 as this
// is the next prime number
for (int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % ( i + 2 ) == 0)
return false;
return true;
}
// Returns true if N is
// a K-Hyperperfect number
// Else returns false.
static boolean isHyperPerfect(int N,
int K)
{
int sum = divisorSum(N, K);
// Condition from the
// definition of hyperperfect
if ((1 + K * (sum)) == N)
return true;
else
return false;
}
// Driver Code
public static void main (String[] args)
{
int N1 = 1570153, K1 = 12;
int N2 = 321, K2 = 3;
// First two statements test
// against the condition
// N = 1 + K*(sum(proper divisors))
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");
}
}
// This code is contributed by ajit
Python3
# Python3 program to check whether a
# given number is k-hyperperfect
import math
# Function to find the sum of all
# proper divisors (excluding 1 and N)
def divisorSum(N, K):
Sum = 0
# Iterate only until sqrt N as we are
# going to generate pairs to produce
# divisors
for i in range(2, math.ceil(math.sqrt(N))):
# As divisors occur in pairs, we can
# take the values i and N/i as long
# as i divides N
if (N % i == 0):
Sum += (i + int(N / i))
return Sum
# Function to check whether the given
# number is prime
def isPrime(n):
# Base and corner cases
if (n == 1 or n == 0):
return False
if (n <= 3):
return True
# Since integers can be represented as
# some 6*k + y where y >= 0, we can eliminate
# all integers that can be expressed in this
# form
if (n % 2 == 0 or n % 3 == 0):
return False
# Start from 5 as this is the next
# prime number
i = 5
while (i * i <= n):
if (n % i == 0 or n % (i + 2) == 0):
return False
i += 6
return True
# Returns true if N is a K-Hyperperfect
# number. Else returns false.
def isHyperPerfect(N, K):
Sum = divisorSum(N, K)
# Condition from the definition
# of hyperperfect
if ((1 + K * (Sum)) == N):
return True
else:
return False
# Driver code
N1 = 1570153
K1 = 12
N2 = 321
K2 = 3
# First two statements test against the condition
# N = 1 + K*(sum(proper divisors))
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 = "")
# This code is contributed by avanitrachhadiya2155
C#
// C# program to check
// whether a given number
// is k-hyperperfect
using System;
class GFG
{
// function to find the
// sum of all proper
// divisors (excluding
// 1 and N)
static int divisorSum(int N,
int K)
{
int sum = 0;
// Iterate only until
// sqrt N as we are
// going to generate
// pairs to produce
// divisors
for (int i = 2 ;
i <= Math.Ceiling(Math.Sqrt(N));
i++)
// As divisors occur in
// pairs, we can take
// the values i and N/i
// as long as i divides N
if (N % i == 0)
sum += (i + N / i);
return sum;
}
// Function to check
// whether the given
// number is prime
static bool isPrime(int n)
{
// base and corner cases
if (n == 1 || n == 0)
return false;
if (n <= 3)
return true;
// Since integers can be
// represented as some
// 6*k + y where y >= 0,
// we can eliminate all
// integers that can be
// expressed in this form
if (n % 2 == 0 ||
n % 3 == 0)
return false;
// start from 5 as this
// is the next prime number
for (int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % ( i + 2 ) == 0)
return false;
return true;
}
// Returns true if N is
// a K-Hyperperfect number
// Else returns false.
static bool isHyperPerfect(int N,
int K)
{
int sum = divisorSum(N, K);
// Condition from the
// definition of hyperperfect
if ((1 + K * (sum)) == N)
return true;
else
return false;
}
// Driver Code
static public void Main ()
{
int N1 = 1570153, K1 = 12;
int N2 = 321, K2 = 3;
// First two statements
// test against the
// condition N = 1 + K*
// (sum(proper divisors))
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");
}
}
// This code is contributed
// by akt_mit
PHP
<?php
// PHP 4.3.2 program to check
// whether a given number is
// k-hyperperfect
// function to find the sum
// of all proper divisors
// (excluding 1 and N)
function divisorSum($N, $K)
{
$sum = 0;
// Iterate only until
// sqrt N as we are
// going to generate
// pairs to produce
// divisors
for ($i = 2 ;
$i <= ceil(sqrt($N)) ; $i++)
// As divisors occur in
// pairs, we can take the
// values i and N/i as long
// as i divides N
if ($N % $i == 0)
$sum += ( $i + $N / $i);
return $sum;
}
// Function to check whether
// the given number is prime
function isPrime($n)
{
// base and corner cases
if ($n == 1 || $n == 0)
return false;
if ($n <= 3)
return true;
// Since integers can be
// represented as some 6*k + y
// where y >= 0, we can
// eliminate all integers that
// can be expressed in this form
if ($n % 2 == 0 || $n % 3 == 0)
return false;
// start from 5 as this
// is the next prime number
for ($i = 5;
$i * $i <= $n; $i = $i + 6)
if ($n % $i == 0 ||
$n % ($i + 2) == 0)
return false;
return true;
}
// Returns true if N is a
// K-Hyperperfect number
// Else returns false.
function isHyperPerfect($N, $K)
{
$sum = divisorSum($N, $K);
// Condition from the
// definition of hyperperfect
if ((1 + $K * ($sum)) == $N)
return true;
else
return false;
}
// Driver Code
$N1 = 1570153;
$K1 = 12;
$N2 = 321;
$K2 = 3;
// First two statements test
// against the condition
// N = 1 + K*(sum(proper divisors))
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";
// This code is contributed
// by akt_mit
?>
JavaScript
<script>
// Javascript program to check
// whether a given number
// is k-hyperperfect
// Function to find the
// sum of all proper
// divisors (excluding
// 1 and N)
function divisorSum(N, K)
{
let sum = 0;
// Iterate only until sqrt N as
// we are going to generate
// pairs to produce divisors
for(let i = 2;
i <= Math.ceil(Math.sqrt(N));
i++)
// As divisors occur in
// pairs, we can take
// the values i and N/i
// as long as i divides N
if (N % i == 0)
sum += (i + parseInt(N / i, 10));
return sum;
}
// Function to check
// whether the given
// number is prime
function isPrime(n)
{
// base and corner cases
if (n == 1 || n == 0)
return false;
if (n <= 3)
return true;
// Since integers can be
// represented as some
// 6*k + y where y >= 0,
// we can eliminate all
// integers that can be
// expressed in this form
if (n % 2 == 0 || n % 3 == 0)
return false;
// Start from 5 as this
// is the next prime number
for(let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Returns true if N is
// a K-Hyperperfect number
// Else returns false.
function isHyperPerfect(N, K)
{
let sum = divisorSum(N, K);
// Condition from the
// definition of hyperperfect
if ((1 + K * (sum)) == N)
return true;
else
return false;
}
// Driver code
let N1 = 1570153, K1 = 12;
let N2 = 321, K2 = 3;
// First two statements
// test against the
// condition N = 1 + K*
// (sum(proper divisors))
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>");
// This code is contributed by decode2207
</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
Similar Reads
Check whether a number is Non-hypotenuse number
Given a positive integer n, the task is to check if n is a Non-hypotenuse number or not. If n is a Non-hypotenuse number then print 'YES' else print 'NO'. Non-hypotenuse number : In mathematics, a Non-hypotenuse number is a natural number whose square can not be expressed as sum of two distinct non-
10 min read
How to check if a given number is Fibonacci number?
Given a number ânâ, how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .. Examples :Input : 8Output : YesInput : 34Output : YesInput : 41Output : NoApproach 1:A simple way is to generate Fibonacci numbers until the generated number
15 min read
Check whether the given number is Euclid Number or not
Given a positive integer n, the task is to check if it is Euclid Number or not. Print âYESâ if the given number is Euclid Number otherwise print âNO'. Euclid number : In Mathematics, Euclid numbers are integers of the form - E_{n} = p_{n}\# + 1 where p_{n}\# is product of first n prime numbers.The f
15 min read
Check if a number is a Trojan Number
Given a Number N . The task is to check if N is a Trojan Number or not.Trojan Number is a number that is a strong number but not a perfect power. A number N is known as a strong number if, for every prime divisor or factor p of N, p2 is also a divisor. In other words, every prime factor appears at l
9 min read
Check if the given number is Ore number or not
Given a positive integer n, check if it is an Ore number or not. Print 'YES' if n is an ore number otherwise print 'NO'.Ore Number: In mathematics, Ore numbers are positive integers whose divisors have an integer harmonic value. Ore numbers are often called harmonic divisor numbers. Ore numbers are
7 min read
Check whether a number is Emirpimes or not
Given a number 'n', check whether it is an emirpimes or not. An emirpimes("semiprime" when spelled backwards) derives its definition from the way it is spelt. So, an emirpimes is a number that is a semiprime (product of two prime numbers) itself, and the reversal of its digits gives another new numb
10 min read
Check whether a number is Sublime number or not
Given a number N, the task is to check whether the number is a Sublime number or not. Sublime numbers are defined as positive numbers whose sum of positive divisors and count of positive divisors are both perfect numbers. Examples: Input: N = 12Output: TrueExplanation: 12 is a sublime number because
6 min read
Check if a number is an Achilles number or not
Given a positive integer N. The task is to check if N is an Achilles number or not. Print âYESâ if N is an Achilles number else print âNOâ. Achilles number: In Mathematics, an Achilles number is a number that is powerful ( A number n is said to be Powerful Number if for every prime factor p of it, p
11 min read
Check if a number is Triperfect Number
Given a number N . The tasks is to check if the given number is a Triperfect Number.Triperfect Number: A Number is a Triperfect Number if it is equal to three times the sum of its divisors, that is, 3 times sum of its positive divisors.Examples: Input: N = 15 Output: false Divisors of 15 are 1, 3, 5
7 min read
Check whether a very large number of the given form is a multiple of 3.
Consider a very long K-digit number N with digits d0, d1, ..., dK-1 (in decimal notation; d0 is the most significant and dK-1 the least significant digit). This number is so large that it can't be given or written down explicitly; instead, only its starting digits are given and a way to construct th
13 min read