Iterating till the square root of the number and checking if the sum of divisors is equal to the number or not
for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } }