📌  相关文章
📜  检查一个数字是否可以表示为两个丰富数字的总和

📅  最后修改于: 2021-05-04 23:01:18             🧑  作者: Mango

给定一个数字N。任务是将N表示为两个丰富数字的总和。如果无法打印-1。

例子:

Input : N = 24 
Output : 12, 12

Input : N = 5
Output : -1

方法:一种有效的方法是将所有数量丰富的数据存储在一个集合中。对于给定的数字N,请运行从1到n的循环,并检查i和ni是否为大量。

下面是上述方法的实现:

C++
// CPP program 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 program 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
?>


输出:
12 12