Sum of all proper divisors of a natural number | Function to calculate sum of all proper divisors num -- > given natural number ; Final result of summation of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Driver Code
< ? php function divSum ( $ num ) { $ result = 0 ; for ( $ i = 2 ; $ i <= sqrt ( $ num ) ; $ i ++ ) { if ( $ num % $ i == 0 ) { if ( $ i == ( $ num / $ i ) ) $ result += $ i ; else $ result += ( $ i + $ num / $ i ) ; } } return ( $ result + 1 ) ; } $ num = 36 ; echo ( divSum ( $ num ) ) ; ? >