Count all perfect divisors of a number | Below is PHP 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
< ? php $ MAX = 10001 ; $ perfectDiv = array_fill ( 0 , $ MAX , 0 ) ; function precomputeCounts ( ) { global $ MAX , $ perfectDiv ; for ( $ i = 1 ; $ i * $ i < $ MAX ; ++ $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i * $ i ) ++ $ perfectDiv [ $ j ] ; } } function countPerfectDivisors ( $ n ) { global $ perfectDiv ; return $ perfectDiv [ $ n ] ; } precomputeCounts ( ) ; $ n = 16 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " . $ n . " ▁ = ▁ " . countPerfectDivisors ( $ n ) . " STRNEWLINE " ; $ n = 12 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " . $ n . " ▁ = ▁ " . countPerfectDivisors ( $ n ) ; ? >