Sum of all proper divisors from 1 to N | Utility function to find sum of all proper divisor of number up to N ; Loop to iterate over all the numbers from 1 to N ; Find all divisors of i and add them ; Subtracting ' i ' so that the number itself is not included ; Driver Code
function properDivisorSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; ++ i ) { for ( let j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } sum = sum - i ; } return sum ; } let n = 4 ; document . write ( properDivisorSum ( n ) + " " ) ; n = 5 ; document . write ( properDivisorSum ( n ) + " " ) ;