
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- C++ 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++
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Swift program to check whether a number can be expressed as sum of two prime numbers
- 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
- Check if a number can be expressed as 2^x + 2^y in C++
- C program for a number to be expressed as a sum of two prime numbers.
- 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++

Advertisements