Check if a number can be expressed as sum two abundant numbers
Given a number N. The task is to express N as the sum of two Abundant Numbers. If it is not possible, print -1.
Examples:
Input : N = 24
Output : 12, 12
Input : N = 5
Output : -1
Approach: An efficient approach is to store all abundant numbers in a set. And for a given number, N runs a loop from 1 to n and checks if i and n-i are abundant numbers or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
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;
}
|
Java
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);
}
}
|
Python 3
from math import sqrt
N = 100005
def ABUNDANT() :
v = set () ;
for i in range ( 1 , N) :
sum = 1
for j in range ( 2 , int (sqrt(i)) + 1 ) :
if (i % j = = 0 ) :
sum + = j
if (i / j ! = j) :
sum + = i / / j
if ( sum > i) :
v.add(i)
return v
def SumOfAbundant(n) :
v = ABUNDANT()
for i in range ( 1 , n + 1 ) :
if ( list (v).count(i) and
list (v).count(n - i)) :
print (i, " " , n - i)
return
print ( - 1 )
if __name__ = = "__main__" :
n = 24
SumOfAbundant(n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static readonly int N = 100005;
static HashSet< int > ABUNDANT()
{
HashSet< int > v = new HashSet< int >();
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)
{
HashSet< int > v = ABUNDANT();
for ( int i = 1; i <= n; i++)
{
if (v.Contains(i) & v.Contains(n - i))
{
Console.Write(i + " " + (n - i));
return ;
}
}
Console.Write(-1);
}
public static void Main()
{
int n = 24;
SumOfAbundant(n);
}
}
|
Javascript
<script>
var N = 100005;
function ABUNDANT()
{
var v = new Set();
var i,j;
for (i = 1; i < N; i++) {
var sum = 1;
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
sum += j;
if (parseInt(i / j) != j)
sum += parseInt(i / j);
}
}
if (sum > i)
v.add(i);
}
return v;
}
function SumOfAbundant(n)
{
var v = new Set();
v = ABUNDANT();
var i;
for (i = 1; i <= n; i++) {
if (v.has(i) && v.has(n - i)) {
document.write(i+ ' ' + (n-i))
return ;
}
}
document.write(-1);
}
var n = 24;
SumOfAbundant(n);
</script>
|
PHP
<?php
function ABUNDANT()
{
$N = 100005;
$v = array ();
for ( $i = 1; $i < $N ; $i ++)
{
$sum = 1;
for ( $j = 2; $j * $j <= $i ; $j ++)
{
if ( $i % $j == 0)
{
$sum += $j ;
if ( $i / $j != $j )
$sum += $i / $j ;
}
}
if ( $sum > $i )
array_push ( $v , $i );
}
$v = array_unique ( $v );
return $v ;
}
function SumOfAbundant( $n )
{
$v = ABUNDANT();
for ( $i = 1; $i <= $n ; $i ++)
{
if (in_array( $i , $v ) &&
in_array( $n - $i , $v ))
{
echo $i , " " , $n - $i ;
return ;
}
}
echo -1;
}
$n = 24;
SumOfAbundant( $n );
?>
|
Time Complexity: O(n2*logn)
Auxiliary Space: O(n)
Another Approach:
- Define a function “isAbundant” to check if a number is an abundant number.
- Iterate from 2 to the square root of the number.
- If the number is divisible by the current iteration, add the divisor and its counterpart to a sum.
- Return true if the sum is greater than the number, indicating it is an abundant number.
- Otherwise, return false.
- Define a function “findAbundantSum” to find two abundant numbers summing up to N.
- Iterate from 1 to N/2
- Check if both the current number and N minus the current number are abundant using the “isAbundant” function.
- If both are abundant, return the pair of numbers.
- If no pair is found, return -1.
- In the main function:
- Set the desired value of N.
- Call the “findAbundantSum” function to get the result.If the result is -1, print -1 indicating no two abundant numbers sum up to N.
- Otherwise, print the pair of abundant numbers.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
bool isAbundant( int num)
{
int sum = 1;
for ( int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
if (i != num / i)
sum += num / i;
}
}
return sum > num;
}
vector< int > findAbundantSum( int N)
{
vector< int > result;
for ( int i = 1; i <= N / 2; i++) {
if (isAbundant(i) && isAbundant(N - i)) {
result.push_back(i);
result.push_back(N - i);
return result;
}
}
result.push_back(-1);
return result;
}
int main()
{
int N = 24;
vector< int > abundantSum = findAbundantSum(N);
if (abundantSum[0] == -1)
cout << -1 << endl;
else
cout << abundantSum[0] << ", " << abundantSum[1]
<< endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
static boolean isAbundant( int num) {
int sum = 1 ;
for ( int i = 2 ; i * i <= num; i++) {
if (num % i == 0 ) {
sum += i;
if (i != num / i)
sum += num / i;
}
}
return sum > num;
}
static List<Integer> findAbundantSum( int N) {
List<Integer> result = new ArrayList<>();
for ( int i = 1 ; i <= N / 2 ; i++) {
if (isAbundant(i) && isAbundant(N - i)) {
result.add(i);
result.add(N - i);
return result;
}
}
result.add(- 1 );
return result;
}
public static void main(String[] args) {
int N = 24 ;
List<Integer> abundantSum = findAbundantSum(N);
if (abundantSum.get( 0 ) == - 1 )
System.out.println(- 1 );
else
System.out.println(abundantSum.get( 0 ) + ", " + abundantSum.get( 1 ));
}
}
|
Python3
def is_abundant(num):
divisors_sum = 1
for i in range ( 2 , int (num * * 0.5 ) + 1 ):
if num % i = = 0 :
divisors_sum + = i
if i ! = num / / i:
divisors_sum + = num / / i
return divisors_sum > num
def find_abundant_sum(N):
result = []
for i in range ( 1 , N / / 2 + 1 ):
if is_abundant(i) and is_abundant(N - i):
result.append(i)
result.append(N - i)
return result
result.append( - 1 )
return result
if __name__ = = "__main__" :
N = 24
abundant_sum = find_abundant_sum(N)
if abundant_sum[ 0 ] = = - 1 :
print ( - 1 )
else :
print (abundant_sum[ 0 ], "," , abundant_sum[ 1 ])
|
C#
using System;
using System.Collections.Generic;
class GFG {
static bool IsAbundant( int num)
{
int sum = 1;
for ( int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
if (i != num / i)
sum += num / i;
}
}
return sum > num;
}
static List< int > FindAbundantSum( int N)
{
List< int > result = new List< int >();
for ( int i = 1; i <= N / 2; i++) {
if (IsAbundant(i) && IsAbundant(N - i)) {
result.Add(i);
result.Add(N - i);
return result;
}
}
result.Add(-1);
return result;
}
static void Main( string [] args)
{
int N = 24;
List< int > abundantSum = FindAbundantSum(N);
if (abundantSum[0] == -1)
Console.WriteLine(
-1);
else
Console.WriteLine(
abundantSum[0] + ", "
+ abundantSum[1]);
}
}
|
Javascript
function isAbundant(num) {
let sum = 1;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) {
sum += i;
if (i !== num / i)
sum += num / i;
}
}
return sum > num;
}
function findAbundantSum(N) {
let result = [];
for (let i = 1; i <= N / 2; i++) {
if (isAbundant(i) && isAbundant(N - i)) {
result.push(i);
result.push(N - i);
return result;
}
}
result.push(-1);
return result;
}
const N = 24;
let abundantSum = findAbundantSum(N);
if (abundantSum[0] === -1)
console.log(-1);
else
console.log(`${abundantSum[0]}, ${abundantSum[1]}`);
|
Time Complexity: O(N * sqrt(N)).
Auxiliary Space: O(1).
Last Updated :
15 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...