Deficient Number | C ++ program to implement an Optimized Solution to check Deficient Number ; Function to calculate sum of divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divisorsSum ( int n ) { for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } bool isDeficient ( int n ) { return ( divisorsSum ( n ) < ( 2 * n ) ) ; } int main ( ) { isDeficient ( 12 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; isDeficient ( 15 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; return 0 ; }