# Check if a number can be expressed as sum two abundant numbers

Given a number N. The task is to express N as sum two Abundant Numbers. If it is not possibe print -1.

Examples:

Input : N = 24
Output : 12, 12

Input : N = 5
Output : -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach : An efficient approach is to store all abundant numbers in a set. And for a given number N run a loop from 1 to n and check if i and n-i are abundant numbers or not.

Below is the implementation of the above approach:

## C++

 // CPP progarm to check if number n is expressed // as sum of two abundant numbers #include using namespace std; #define N 100005    // Function to return all abundant numbers // This function will be helpful for // multiple queries set ABUNDANT() {     // To store abundant numbers     set v;        for (int i = 1; i < N; i++) {            // to store sum of the divisors         // include 1 in the sum         int sum = 1;         for (int j = 2; j * j <= i; j++) {                // if j is proper divisor             if (i % j == 0) {                 sum += j;                    // if i is not a perfect square                 if (i / j != j)                     sum += i / j;             }         }            // if sum is greater than i then i is         // a abundant number         if (sum > i)             v.insert(i);     }        return v; }    // Check if number n is expressed // as sum of two abundant numbers void SumOfAbundant(int n) {     set v = ABUNDANT();        for (int i = 1; i <= n; i++) {            // if both i and n-i are         // abundant numbers         if (v.count(i) and v.count(n - i)) {             cout << i << " " << n - i;             return;         }     }        // can not be expressed     cout << -1; }    // Driver code int main() {     int n = 24;     SumOfAbundant(n);     return 0; }

## Java

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

## Python 3

 # Python 3 progarm to check if number n is  # expressed as sum of two abundant numbers     # from math lib import sqrt function from math import sqrt     N = 100005    # Function to return all abundant numbers  # This function will be helpful for  # multiple queries  def ABUNDANT() :        # To store abundant numbers      v = set() ;         for i in range(1, N) :             # to store sum of the divisors          # include 1 in the sum          sum = 1         for j in range(2, int(sqrt(i)) + 1) :                            # if j is proper divisor             if (i % j == 0) :                 sum += j                                # if i is not a perfect square              if (i / j != j) :                 sum += i // j                            # if sum is greater than i then i          # is a abundant numbe         if (sum > i) :             v.add(i)            return v    # Check if number n is expressed  # as sum of two abundant numbers  def SumOfAbundant(n) :      v = ABUNDANT()            for i in range(1, n + 1) :            # if both i and n-i are abundant numbers          if (list(v).count(i) and              list(v).count(n - i)) :              print(i, " ", n - i)              return                    # can not be expressed     print(-1)        # Driver code  if __name__ == "__main__" :     n = 24     SumOfAbundant(n)    # This code is contributed by Ryuga

## C#

 // C# program to check if number n is expressed // as sum of two abundant numbers using System; using System.Collections.Generic;    class GFG {        static readonly int N = 100005;        // Function to return all abundant numbers     // This function will be helpful for     // multiple queries     static HashSet ABUNDANT()      {         // To store abundant numbers         HashSet v = new HashSet();            for (int i = 1; i < N; i++)         {                // to store sum of the divisors             // include 1 in the sum             int sum = 1;             for (int j = 2; j * j <= i; j++)             {                    // if j is proper divisor                 if (i % j == 0)                  {                     sum += j;                        // if i is not a perfect square                     if (i / j != j)                     {                         sum += i / j;                     }                 }             }                // if sum is greater than i then i is             // a abundant number             if (sum > i)              {                 v.Add(i);             }         }         return v;     }        // Check if number n is expressed     // as sum of two abundant numbers     static void SumOfAbundant(int n)      {         HashSet v = ABUNDANT();            for (int i = 1; i <= n; i++)          {                // if both i and n-i are             // abundant numbers             if (v.Contains(i) & v.Contains(n - i))              {                 Console.Write(i + " " + (n - i));                 return;             }         }            // can not be expressed         Console.Write(-1);     }        // Driver code     public static void Main()     {         int n = 24;         SumOfAbundant(n);     } }    // This code is contributed by PrinciRaj1992

## PHP

\$i)
array_push(\$v, \$i);
}
\$v = array_unique(\$v);
return \$v;
}

// Check if number n is expressed
// as sum of two abundant numbers
function SumOfAbundant(\$n)
{
\$v = ABUNDANT();

for (\$i = 1; \$i <= \$n; \$i++) { // if both i and n-i are // abundant numbers if (in_array(\$i, \$v) && in_array(\$n - \$i, \$v)) { echo \$i, " ", \$n - \$i; return; } } // can not be expressed echo -1; } // Driver code \$n = 24; SumOfAbundant(\$n); // This code is contributed // by Arnab Kundu ?>

Output:

12 12

My Personal Notes arrow_drop_up