Check if a number can be expressed as sum two abundant numbers | Java program to check if number n is expressed as sum of two abundant numbers ; Function to return all abundant numbers This function will be helpful for multiple queries ; To store abundant numbers ; to store sum of the divisors include 1 in the sum ; if j is proper divisor ; if i is not a perfect square ; if sum is greater than i then i is a abundant number ; Check if number n is expressed as sum of two abundant numbers ; if both i and n - i are abundant numbers ; can not be expressed ; Driver code
import java . util . * ; class GFG { static final int N = 100005 ; static Set < Integer > ABUNDANT ( ) { Set < Integer > v = new HashSet < > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { int sum = 1 ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { sum += j ; if ( i / j != j ) { sum += i / j ; } } } if ( sum > i ) { v . add ( i ) ; } } return v ; } static void SumOfAbundant ( int n ) { Set < Integer > v = ABUNDANT ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( v . contains ( i ) & v . contains ( n - i ) ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; return ; } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 24 ; SumOfAbundant ( n ) ; } }