Check if a number can be expressed as sum two abundant numbers | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE set < int > ABUNDANT ( ) { set < int > v ; 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 . insert ( i ) ; } return v ; } void SumOfAbundant ( int n ) { set < int > v = ABUNDANT ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( v . count ( i ) and v . count ( n - i ) ) { cout << i << " ▁ " << n - i ; return ; } } cout << -1 ; } int main ( ) { int n = 24 ; SumOfAbundant ( n ) ; return 0 ; }