Highly Composite Numbers

A number N is called highly composite if it has more divisors than any of the 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 than 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:  



  1. Find the count of divisors of N
  2. Now in a loop from 1 to less than N, check for every i that if a number of divisors of i are more than the count of divisors of N, then return false
  3. Otherwise, return true at the end.

Below is the implementation of the above approach: 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation for checking
// Highly Composite Number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number
// of divisors of the N
int divCount(int n)
{
    // sieve method for prime calculation
    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;
 
    // Traversing through
    // all prime numbers
    int total = 1;
    for (int p = 2; p <= n; p++) {
        if (hash[p]) {
 
            // calculate number of divisor
            // with formula total div =
            // (p1+1) * (p2+1) *.....* (pn+1)
            // where n = (a1^p1)*(a2^p2)....
            // *(an^pn) ai being prime divisor
            // for n and pi are their respective
            // power in factorization
            int count = 0;
            if (n % p == 0) {
                while (n % p == 0) {
                    n = n / p;
                    count++;
                }
                total = total * (count + 1);
            }
        }
    }
    return total;
}
 
// Function to check if a number
// is a highly composite number
bool isHighlyCompositeNumber(int N)
{
    // count number of factors of N
    int NdivCount = divCount(N);
 
    // loop to count number of factors of
    // every number less than N
    for (int i = 1; i < N; i++) {
        int idivCount = divCount(i);
 
        // If any number less than N has
        // more factors than N,
        // then return false
        if (idivCount >= NdivCount)
            return false;
    }
 
    return true;
}
 
// Driver code
int main()
{
    // Given Number N
    int N = 12;
 
    // Function Call
    if (isHighlyCompositeNumber(N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation for checking
// Highly Composite Number
import java.util.*;
class GFG{
  
// Function to count the number
// of divisors of the N
static int divCount(int n)
{
    // sieve method for prime calculation
    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;
  
    // Traversing through
    // all prime numbers
    int total = 1;
    for (int p = 2; p <= n; p++)
    {
        if (hash[p])
        {
  
            // calculate number of divisor
            // with formula total div =
            // (p1+1) * (p2+1) *.....* (pn+1)
            // where n = (a1^p1)*(a2^p2)....
            // *(an^pn) ai being prime divisor
            // for n and pi are their respective
            // power in factorization
            int count = 0;
            if (n % p == 0)
            {
                while (n % p == 0)
                {
                    n = n / p;
                    count++;
                }
                total = total * (count + 1);
            }
        }
    }
    return total;
}
  
// Function to check if a number
// is a highly composite number
static boolean isHighlyCompositeNumber(int N)
{
    // count number of factors of N
    int NdivCount = divCount(N);
  
    // loop to count number of factors of
    // every number less than N
    for (int i = 1; i < N; i++)
    {
        int idivCount = divCount(i);
  
        // If any number less than N has
        // more factors than N,
        // then return false
        if (idivCount >= NdivCount)
            return false;
    }
    return true;
}
  
// Driver code
public static void main(String[] args)
{
    // Given Number N
    int N = 12;
  
    // Function Call
    if (isHighlyCompositeNumber(N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by gauravrajput1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation for checking
// Highly Composite Number
using System;
class GFG{
  
// Function to count the number
// of divisors of the N
static int divCount(int n)
{
    // sieve method for prime calculation
    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;
  
    // Traversing through
    // all prime numbers
    int total = 1;
    for (int p = 2; p <= n; p++)
    {
        if (hash[p])
        {
  
            // calculate number of divisor
            // with formula total div =
            // (p1+1) * (p2+1) *.....* (pn+1)
            // where n = (a1^p1)*(a2^p2)....
            // *(an^pn) ai being prime divisor
            // for n and pi are their respective
            // power in factorization
            int count = 0;
            if (n % p == 0)
            {
                while (n % p == 0)
                {
                    n = n / p;
                    count++;
                }
                total = total * (count + 1);
            }
        }
    }
    return total;
}
  
// Function to check if a number
// is a highly composite number
static bool isHighlyCompositeNumber(int N)
{
    // count number of factors of N
    int NdivCount = divCount(N);
  
    // loop to count number of factors of
    // every number less than N
    for (int i = 1; i < N; i++)
    {
        int idivCount = divCount(i);
  
        // If any number less than N has
        // more factors than N,
        // then return false
        if (idivCount >= NdivCount)
            return false;
    }
    return true;
}
  
// Driver code
public static void Main(String[] args)
{
    // Given Number N
    int N = 12;
  
    // Function Call
    if (isHighlyCompositeNumber(N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by shikhasingrajput

chevron_right


Output: 

Yes



 

Time Complexity: O(n) 
Reference: http://www.numbersaplenty.com/set/highly_composite_number/
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.