Abundant Number | An Optimized Solution to check Abundant Number in PYTHON ; 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 ; calculate sum of all proper divisors only ; Function to check Abundant Number ; Return true if sum of divisors is greater than n . ; Driver program to test above function
import math NEW_LINE def getSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 1 NEW_LINE while i <= ( math . sqrt ( n ) ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT if n / i == i : NEW_LINE INDENT sum = sum + i NEW_LINE sum = sum + i NEW_LINE sum = sum + ( n / i ) NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE DEDENT sum = sum - n NEW_LINE return sum NEW_LINE DEDENT def checkAbundant ( n ) : NEW_LINE INDENT if ( getSum ( n ) > n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( checkAbundant ( 12 ) == 1 ) : NEW_LINE INDENT print " YES " NEW_LINE DEDENT else : NEW_LINE INDENT print " NO " NEW_LINE DEDENT if ( checkAbundant ( 15 ) == 1 ) : NEW_LINE INDENT print " YES " NEW_LINE DEDENT else : NEW_LINE INDENT print " NO " NEW_LINE DEDENT