Skip to content
Related Articles

Related Articles

Check Whether a Number is an Anti Prime Number(Highly Composite Number)

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 01 Sep, 2022
Improve Article
Save Article

Given a positive integer N, the task is to tell whether it’s an anti-prime number or not.

Anti-Prime Numbers (Highly Composite Number):

A positive integer that has more divisors than any positive number smaller than it, is called an Anti-Prime Number (also known as Highly Composite Number). 

Following is the list of the first 10 anti-prime numbers along with their prime factorizations:

Anti-Prime Number Prime Factorization
1  
2 2
4 22
6 2×3
12 22×3
24 23×3
36 22×32
48 24×3
60 22×3×5
120 23×3×5

Examples:

Input: N = 5040
Output: 5040 is anti-prime
Explanation: There is no positive integer less than 5040 having 
number of divisors more than or equal to the number of divisors of 5040.

Input: N = 72
Output: 72 is not anti-prime

Approach:

This question can be solved by counting the number of divisors of the current number and then counting the number of divisors for each number less than it and checking whether any number has the number of divisors greater than or equal to the number of divisors of N.

Follow the steps to solve this problem:

  • Find how many factors this number has.
  • Now iterate till  N-1 and check
    • Does any number less than the number has factors more or equal to the  number 
    • If yes, then the number is not an anti-prime number.
  • If in the end none of the numbers have the number of divisors greater than or equal to the number of divisors of N, then N is an anti-prime number.

Below is the implementation of the above approach.

C++




// C++ code to implement the above mentioned approach
#include <bits/stdc++.h>
using namespace std;
 
// function to find divisors of a number
int Divisors(int a)
{
    if (a == 1)
        return 1;
 
    // Now Finding number of all the factors
    int f = 2;
    for (int i = 2; i * i <= a; i++) {
        if (a % i == 0) {
            if (i * i != a) {
                f += 2;
            }
            else
                f++;
        }
    }
    return f;
}
 
// Function to check number is Anti Prime(Highly
// Composite) Number or not.
bool isAntiPrime(int n)
{
    int init = Divisors(n);
    for (int i = 1; i < n; i++) {
        if (Divisors(i) >= init)
 
            return false;
    }
    return true;
}
 
int main()
{
 
    int N = 5040;
 
    // Function Call
    if (isAntiPrime(N))
        cout << N << " is anti-prime\n";
    else
        cout << N << " is not anti-prime\n";
}
 
// This code is contributed by ishankhandelwals.


Java




// Java code to implement the above mentioned approach
class GFG {
 
    // Function to check number is Anti Prime(Highly
    // Composite) Number or not.
    public static boolean isAntiPrime(int n)
    {
        int init = Divisors(n);
        for (int i = 1; i < n; i++) {
            if (Divisors(i) >= init)
 
                return false;
        }
        return true;
    }
 
    // function to find divisors of a number
    public static int Divisors(int a)
    {
        if (a == 1)
            return 1;
 
        // Now Finding number of all the factors
        int f = 2;
        for (int i = 2; i * i <= a; i++) {
            if (a % i == 0) {
                if (i * i != a) {
                    f += 2;
                }
                else
                    f++;
            }
        }
        return f;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int N = 5040;
 
        // Function Call
        if (isAntiPrime(N))
            System.out.println(N + " is anti-prime");
        else
            System.out.println(N + " is not anti-prime");
    }
}


Python3




from math import sqrt
# python code to implement the above mentioned approach
# function to find divisors of a number
def Divisors(a):
  if(a == 1):
    return 1
   
# Now Finding number of all the factors
  f = 2
  for i in range(2,int(sqrt(a)) + 1):
    if(a % i == 0):
      if(i * i != a):
        f += 2
      else:
        f += 1
  return f
 
# Function to check number is Anti Prime(Highly
# Composite) Number or not.
def isAntiPrime(n):
  init = Divisors(n)
  for i in range(n):
    if(Divisors(i) >= init):
      return False
  return True
 
N = 5040
 
# Function Call
if (isAntiPrime(N)):
  print(N, " is anti-prime ")
else:
  print(N, " is not anti-prime ")
   
# This code is contributed by Atul_kumar_Shrivastava


C#




// C# code to implement the approach
using System;
 
class GFG {
 
  // Function to check number is Anti Prime(Highly
  // Composite) Number or not.
  public static bool isAntiPrime(int n)
  {
    int init = Divisors(n);
    for (int i = 1; i < n; i++) {
      if (Divisors(i) >= init)
 
        return false;
    }
    return true;
  }
 
  // function to find divisors of a number
  public static int Divisors(int a)
  {
    if (a == 1)
      return 1;
 
    // Now Finding number of all the factors
    int f = 2;
    for (int i = 2; i * i <= a; i++) {
      if (a % i == 0) {
        if (i * i != a) {
          f += 2;
        }
        else
          f++;
      }
    }
    return f;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 5040;
 
    // Function Call
    if (isAntiPrime(N))
      Console.Write(N + " is anti-prime");
    else
      Console.Write(N + " is not anti-prime");
  }
}
 
// This code is contributed by sanjoy_62.


Javascript




<script>
     // JavaScript code to implement the above mentioned approach
     // function to find divisors of a number
     function Divisors(a){
       if(a == 1)
         return 1;
        
     // Now Finding number of all the factors
     let f = 2;
     for (let i = 2; i < Math.floor(Math.sqrt(a) + 1); i++) {
         if(a % i == 0) {
           if(i * i != a)
     f += 2;
           else
     f += 1;
            }
     }
       return f;
     }
      
     // Function to check number is Anti Prime(Highly
     // Composite) Number or not.
     function isAntiPrime(n){
       init = Divisors(n);   
         for(let i = 0; i < n ; i++) { 
         if(Divisors(i) >= init)
           return false;
         }
            
       return true;
     }
      
      
     let N = 5040;
      
     // Function Call
     if (isAntiPrime(N))
       document.write(N, " is anti-prime ");
     else
        document.write(N, " is not anti-prime ");
        
     // This code is contributed by AnkThon
 </script>


Output

5040 is anti-prime

Time Complexity: O(N3/2)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!