📜  数组中所有理想数的总和

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

给定一个包含N个正整数的数组arr [] 。任务是从数组中找到所有理想数的和。
如果一个数字等于其适当除数的总和,即其正除数的总和(不包括数字本身),则该数字是完美的。

例子:

方法:初始化sum = 0 ,对于数组的每个元素,找到其适当除数的总和,即sumFactors 。如果arr [i] = sumFactors,则将结果总和更新为sum = sum + arr [i] 。最后打印总和

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to return the sum of
// all the proper factors of n
int sumOfFactors(int n)
{
    int sum = 0;
    for (int f = 1; f <= n / 2; f++) 
    {
  
        // f is the factor of n
        if (n % f == 0) 
        {
            sum += f;
        }
    }
    return sum;
}
  
// Function to return the required sum
int getSum(int arr[], int n)
{
  
    // To store the sum
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
  
        // If current element is non-zero and equal
        // to the sum of proper factors of itself
        if (arr[i] > 0 && 
            arr[i] == sumOfFactors(arr[i])) 
        {
            sum += arr[i];
        }
    }
    return sum;
}
  
// Driver code
int main() 
{
    int arr[10] = { 17, 6, 10, 6, 4 };
      
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (getSum(arr, n));
    return 0;
}


Java
// Java implementation of the above approach
class GFG {
  
    // Function to return the sum of
    // all the proper factors of n
    static int sumOfFactors(int n)
    {
        int sum = 0;
        for (int f = 1; f <= n / 2; f++) {
  
            // f is the factor of n
            if (n % f == 0) {
                sum += f;
            }
        }
        return sum;
    }
  
    // Function to return the required sum
    static int getSum(int[] arr, int n)
    {
  
        // To store the sum
        int sum = 0;
        for (int i = 0; i < n; i++) {
  
            // If current element is non-zero and equal
            // to the sum of proper factors of itself
            if (arr[i] > 0 && arr[i] == sumOfFactors(arr[i])) {
                sum += arr[i];
            }
        }
        return sum;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 17, 6, 10, 6, 4 };
        int n = arr.length;
        System.out.print(getSum(arr, n));
    }
}


Python3
# Python3 implementation of the above approach
  
# Function to return the sum of
# all the proper factors of n
def sumOfFactors(n):
  
    sum = 0
    for f in range(1, n // 2 + 1): 
  
        # f is the factor of n
        if (n % f == 0):
            sum += f
          
    return sum
  
# Function to return the required sum
def getSum(arr, n):
      
    # To store the sum
    sum = 0
    for i in range(n):
  
        # If current element is non-zero and equal
        # to the sum of proper factors of itself
        if (arr[i] > 0 and 
            arr[i] == sumOfFactors(arr[i])) :
            sum += arr[i]
      
    return sum
  
# Driver code
arr = [17, 6, 10, 6, 4]
  
n = len(arr)
print(getSum(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using System;
  
class GFG
{
      
    // Function to return the sum of
    // all the proper factors of n
    static int sumOfFactors(int n)
    {
        int sum = 0;
        for (int f = 1; f <= n / 2; f++) 
        {
  
            // f is the factor of n
            if (n % f == 0) 
            {
                sum += f;
            }
        }
        return sum;
    }
  
    // Function to return the required sum
    static int getSum(int[] arr, int n)
    {
  
        // To store the sum
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
  
            // If current element is non-zero and equal
            // to the sum of proper factors of itself
            if (arr[i] > 0 && arr[i] == sumOfFactors(arr[i])) 
            {
                sum += arr[i];
            }
        }
        return sum;
    }
  
    // Driver code
    static public void Main ()
    {
        int[] arr = { 17, 6, 10, 6, 4 };
        int n = arr.Length;
        Console.WriteLine(getSum(arr, n));
    }
}
  
// This code is contributed by @ajit_0023


输出:
12