Highly Composite Numbers
Last Updated :
29 Feb, 2024
A number N is highly composite if it has more divisors than any smaller numbers than N.
Few Highly composite numbers are:
1, 2, 4, 6, 12, 24, 36, 48, 60, 120….
Check if N is a Highly Composite number.
Given a number N, the task is to check if N is a Highly Composite Number or not. If N is a Highly Composite Number, then print “Yes”. Else print “No”.
Examples:
Input: N = 60
Output: Yes
60 is a highly composite because it has 12 divisors
and none of the numbers up to 59 has 12 or more divisors.
Input: N = 18
Output: No
Approach:
- Find the count of divisors of N
- Now, in a loop from 1 to less than N, check for every i that if several divisors of i are more than the count of divisors of N, then return false.
- Otherwise, return true at the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int divCount( int n)
{
bool hash[n + 1];
memset (hash, true , sizeof (hash));
for ( int p = 2; p * p < n; p++)
if (hash[p] == true )
for ( int i = p * 2; i < n; i += p)
hash[i] = false ;
int total = 1;
for ( int p = 2; p <= n; p++) {
if (hash[p]) {
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
bool isHighlyCompositeNumber( int N)
{
int NdivCount = divCount(N);
for ( int i = 1; i < N; i++) {
int idivCount = divCount(i);
if (idivCount >= NdivCount)
return false ;
}
return true ;
}
int main()
{
int N = 12;
if (isHighlyCompositeNumber(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int divCount( int n)
{
boolean []hash = new boolean [n + 1 ];
Arrays.fill(hash, true );
for ( int p = 2 ; p * p < n; p++)
if (hash[p] == true )
for ( int i = p * 2 ; i < n; i += p)
hash[i] = false ;
int total = 1 ;
for ( int p = 2 ; p <= n; p++)
{
if (hash[p])
{
int count = 0 ;
if (n % p == 0 )
{
while (n % p == 0 )
{
n = n / p;
count++;
}
total = total * (count + 1 );
}
}
}
return total;
}
static boolean isHighlyCompositeNumber( int N)
{
int NdivCount = divCount(N);
for ( int i = 1 ; i < N; i++)
{
int idivCount = divCount(i);
if (idivCount >= NdivCount)
return false ;
}
return true ;
}
public static void main(String[] args)
{
int N = 12 ;
if (isHighlyCompositeNumber(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def divCount(n):
Hash = [ True for i in range (n + 1 )]
p = 2
while ((p * p) < n):
if bool ( Hash [p]):
i = p * 2
while i < n:
Hash [i] = False
i + = p
p + = 1
total = 1
for P in range ( 2 , n + 1 ):
if ( bool ( Hash [P])):
count = 0
if (n % P = = 0 ):
while (n % P = = 0 ):
n = n / / P
count + = 1
total = total * (count + 1 )
return total
def isHighlyCompositeNumber(N):
NdivCount = divCount(N)
for i in range (N):
idivCount = divCount(i)
if (idivCount > = NdivCount):
return bool ( False )
return bool ( True )
N = 12
if ( bool (isHighlyCompositeNumber(N))):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int divCount( int n)
{
bool []hash = new bool [n + 1];
for ( int i = 0; i < n + 1; i++)
hash[i] = true ;
for ( int p = 2; p * p < n; p++)
if (hash[p] == true )
for ( int i = p * 2; i < n; i += p)
hash[i] = false ;
int total = 1;
for ( int p = 2; p <= n; p++)
{
if (hash[p])
{
int count = 0;
if (n % p == 0)
{
while (n % p == 0)
{
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
static bool isHighlyCompositeNumber( int N)
{
int NdivCount = divCount(N);
for ( int i = 1; i < N; i++)
{
int idivCount = divCount(i);
if (idivCount >= NdivCount)
return false ;
}
return true ;
}
public static void Main(String[] args)
{
int N = 12;
if (isHighlyCompositeNumber(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function divCount( n) {
let hash = Array(n+1).fill( true );
for ( let p = 2; p * p < n; p++)
if (hash[p] == true )
for ( i = p * 2; i < n; i += p)
hash[i] = false ;
let total = 1;
for ( p = 2; p <= n; p++) {
if (hash[p]) {
let count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
function isHighlyCompositeNumber( N) {
let NdivCount = divCount(N);
for ( let i = 1; i < N; i++) {
let idivCount = divCount(i);
if (idivCount >= NdivCount)
return false ;
}
return true ;
}
let N = 12;
if (isHighlyCompositeNumber(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n*n*log(log(n))
Auxiliary Space: O(n)
Reference: http://www.numbersaplenty.com/set/highly_composite_number/
Share your thoughts in the comments
Please Login to comment...