- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

Suppose we have a number. We have to express this as sum of two Abundant number, if yes, print the numbers, otherwise print -1. A number is said to be Abundant number is sum of all proper divisor of the number, denoted by sum(n) is greater than the value of number.

To solve this, we will store all abundant numbers into a set, and for given number n, run a loop for i = 1 to n, and check n and (n – i) are abundant or not.

## Example

#include <iostream> #include <set> #define N 100005 using namespace std; set<int> getAbundantSet() { set<int> abundant_set; 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) abundant_set.insert(i); } return abundant_set; } void representSumAbundant(int number){ set<int> abundant_set = getAbundantSet(); for (int i = 1; i <= number; i++) { if (abundant_set.count(i) && abundant_set.count(number - i)) { cout << i << " " << number - i; return; } } cout << -1; } int main() { int n = 30; representSumAbundant(n); }

## Output

12 18

- Related Questions & Answers
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if a number can be expressed as a sum of consecutive numbers in C++
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Check if a number can be expressed as power in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as a^b in Python
- Check if an integer can be expressed as a sum of two semi-primes in Python
- C program for a number to be expressed as a sum of two prime numbers.
- Check if a number can be expressed as 2^x + 2^y in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if a number can be expressed as x^y (x raised to power y) in C++
- Check if a number can be written as sum of three consecutive integers in C++
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Program to check a number can be written as a sum of distinct factorial numbers or not in Python

Advertisements