Sum of all divisors from 1 to n | Utility function to find sum of all divisor of number up to ' n ' ; Find all divisors of i and add them ; Driver code
< ? php function divisorSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { for ( $ j = 1 ; $ j * $ j <= $ i ; ++ $ j ) { if ( $ i % $ j == 0 ) { if ( $ i / $ j == $ j ) $ sum += $ j ; else $ sum += $ j + $ i / $ j ; } } } return $ sum ; } $ n = 4 ; echo " " , ▁ divisorSum ( $ n ) , ▁ " " $ n = 5 ; echo divisorSum ( $ n ) , " STRNEWLINE " ; ? >