Sum of all perfect numbers present in an array
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 sum of its proper divisors i.e. sum of its positive divisors excluding the number itself.
Examples:
Input: arr[] = {3, 6, 9}
Output:
Proper 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: Initialise 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++
// C++ implementation of the above approach #include <iostream> 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
Recommended Posts:
- Sum of all mersenne numbers present in an array
- Sum of all palindrome numbers present in an Array
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Maximum consecutive numbers present in an array
- Count numbers upto N which are both perfect square and perfect cube
- Check if product of array containing prime numbers is a perfect square
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Print n numbers such that their sum is a perfect square
- Number of perfect squares between two given numbers
- First string from the given array whose reverse is also present in the same array
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Probability of a key K present in array
- Count elements present in first array but not in second
- Find elements which are present in first array and not in second
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



