Highly Composite Numbers | Function to count the number of divisors of the N ; sieve method for prime calculation ; Traversing through all prime numbers ; 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 ; Function to check if a number is a highly composite number ; count number of factors of N ; loop to count number of factors of every number less than N ; If any number less than N has more factors than N , then return false ; Given Number N ; Function Call
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 ( " " ) ; else document . write ( " " ) ;