Count all perfect divisors of a number | Javascript code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
let MAX = 100001 ; ; let perfectDiv = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { perfectDiv [ i ] = 0 ; } function precomputeCounts ( ) { for ( let i = 1 ; i * i < MAX ; ++ i ) { for ( let j = i * i ; j < MAX ; j += i * i ) ++ perfectDiv [ j ] ; } } function countPerfectDivisors ( n ) { return perfectDiv [ n ] ; } precomputeCounts ( ) ; let n = 16 ; document . write ( " " + n + " " + countPerfectDivisors ( n ) + " " ) ; n = 12 ; document . write ( " " + n + " " + countPerfectDivisors ( n ) ) ;