Abundant Number | An Optimized Solution to check Abundant Number in JAVA ; 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 java . io . * ; import java . math . * ; class GFG { static int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } sum = sum - n ; return sum ; } static boolean checkAbundant ( int n ) { return ( getSum ( n ) > n ) ; } public static void main ( String args [ ] ) throws IOException { if ( checkAbundant ( 12 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; if ( checkAbundant ( 15 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }