Sum of all perfect numbers present in an array - GeeksforGeeks

26 Apr, 2021
Given an array arr[] containing N positive integer. The task is to find the sum of all the perfect numbers from the array. A number is perfect if is equal to the sum of its proper divisors i.e. the sum of its positive divisors excluding the number itself. 
Examples:
Input: arr[] = {3, 6, 9} Output: 6Proper divisor sum of 3 = 1 Proper divisor sum of 6 = 1 + 2 + 3 = 6 Proper divisor sum of 9 = 1 + 3 = 4Input: arr[] = {17, 6, 10, 6, 4} Output: 12  
Approach: Initialize sum = 0 and for every element of the array, find the sum of its proper divisors say sumFactors. If arr[i] = sumFactors then update the resultant sum as sum = sum + arr[i]. Print the sum in the end. 
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <iostream>using namespace std; // Function to return the sum of// all the proper factors of nint 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 sumint 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 codeint main(){    int arr[10] = { 17, 6, 10, 6, 4 };         int n = sizeof(arr) / sizeof(arr[0]);    cout << (getSum(arr, n));    return 0;}
// Java implementation of the above approachclass 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 implementation of the above approach # Function to return the sum of# all the proper factors of ndef 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 sumdef 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 codearr = [17, 6, 10, 6, 4] n = len(arr)print(getSum(arr, n)) # This code is contributed by Mohit Kumar
// C# implementation of the above approachusing 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
<script>// Java  script implementation of the above approach     // Function to return the sum of    // all the proper factors of n    function sumOfFactors( n)    {        let sum = 0;        for (let 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    function getSum( arr,  n)    {         // To store the sum        let sum = 0;        for (let 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        let arr = [ 17, 6, 10, 6, 4 ];        let  n = arr.length;        document.write(getSum(arr, n));     //contributed by bobby </script>
12
 
jit_t
mohit kumar 29
gottumukkalabobby
number-theory
Arrays
Mathematical
School Programming
Arrays
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for Bridge and Torch problem
Window Sliding Technique
Binary Tree (Array implementation)
Segment Tree | Set 1 (Sum of given range)
MSD( Most Significant Digit ) Radix Sort
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Modulo Operator (%) in C/C++ with Examples