Check Whether a Number is an Anti Prime Number(Highly Composite Number)
Last Updated :
01 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int Divisors( int a)
{
if (a == 1)
return 1;
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;
}
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;
if (isAntiPrime(N))
cout << N << " is anti-prime\n" ;
else
cout << N << " is not anti-prime\n" ;
}
|
Java
class GFG {
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 ;
}
public static int Divisors( int a)
{
if (a == 1 )
return 1 ;
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;
}
public static void main(String args[])
{
int N = 5040 ;
if (isAntiPrime(N))
System.out.println(N + " is anti-prime" );
else
System.out.println(N + " is not anti-prime" );
}
}
|
Python3
from math import sqrt
def Divisors(a):
if (a = = 1 ):
return 1
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
def isAntiPrime(n):
init = Divisors(n)
for i in range (n):
if (Divisors(i) > = init):
return False
return True
N = 5040
if (isAntiPrime(N)):
print (N, " is anti-prime " )
else :
print (N, " is not anti-prime " )
|
C#
using System;
class GFG {
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 ;
}
public static int Divisors( int a)
{
if (a == 1)
return 1;
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;
}
public static void Main()
{
int N = 5040;
if (isAntiPrime(N))
Console.Write(N + " is anti-prime" );
else
Console.Write(N + " is not anti-prime" );
}
}
|
Javascript
<script>
function Divisors(a){
if (a == 1)
return 1;
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 isAntiPrime(n){
init = Divisors(n);
for (let i = 0; i < n ; i++) {
if (Divisors(i) >= init)
return false ;
}
return true ;
}
let N = 5040;
if (isAntiPrime(N))
document.write(N, " is anti-prime " );
else
document.write(N, " is not anti-prime " );
</script>
|
Output
5040 is anti-prime
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...