Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number
Last Updated :
29 Jun, 2021
Given an array arr[] of size N, the task is to print all the perfect numbers from an array whose sum of digits is also a perfect number.
Examples:
Input: arr[] = { 3, 8, 12, 28, 6 }
Output: 6
Explanation: The array element arr[4] (= 6) is a perfect number. The array element arr[3] (= 28) is a perfect number but its sum of digits (= 10) is not a perfect number.
Input: arr[] = { 1, 2, 3 }
Output: 1
Approach: Follow the steps below to solve the problem:
- Declare a function, isPerfect() to check if the number is a perfect number or not.
- Declare another function, sumOfDigits() to calculate the sum of all the digits of a number.
- Traverse the array arr[]:
- If arr[i] is a perfect number:
- Initialize a variable, say digitSum, to store the sum of digits of the current array element.
- If digitSum is also a perfect number, print that number.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a number
// is perfect number or not
int isPerfect(int N)
{
// Stores sum of proper divisors
int sumOfDivisors = 1;
for (int i = 2; i <= N / 2; ++i) {
if (N % i == 0) {
sumOfDivisors += i;
}
}
// If sum of digits is equal to N,
// then it's a perfect number
if (sumOfDivisors == N) {
return 1;
}
// Otherwise, not a perfect number
else
return 0;
}
// Function to find the
// sum of digits of a number
int sumOfDigits(int N)
{
// Stores sum of digits
int sum = 0;
while (N != 0) {
sum += (N % 10);
N = N / 10;
}
// Return sum of digits
return sum;
}
// Function to count perfect numbers from
// an array whose sum of digits is also perfect
void countPerfectNumbers(int arr[], int N)
{
// Traverse the array
for (int i = 0; i < N; ++i) {
// If number is perfect
if (isPerfect(arr[i])) {
// Stores sum of digits
// of the number
int sum = sumOfDigits(arr[i]);
// If that is also perfect number
if (isPerfect(sum)) {
// Print that number
cout << arr[i] << " ";
}
}
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 3, 8, 12, 28, 6 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to count perfect numbers
// having sum of digits also perfect
countPerfectNumbers(arr, N);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to check if a number
// is perfect number or not
static boolean isPerfect(int N)
{
// Stores sum of proper divisors
int sumOfDivisors = 1;
for (int i = 2; i <= N / 2; ++i) {
if (N % i == 0) {
sumOfDivisors += i;
}
}
// If sum of digits is equal to N,
// then it's a perfect number
if (sumOfDivisors == N) {
return true;
}
// Otherwise, not a perfect number
else
return false;
}
// Function to find the
// sum of digits of a number
static int sumOfDigits(int N)
{
// Stores sum of digits
int sum = 0;
while (N != 0) {
sum += (N % 10);
N = N / 10;
}
// Return sum of digits
return sum;
}
// Function to count perfect numbers from
// an array whose sum of digits is also perfect
static void countPerfectNumbers(int arr[], int N)
{
// Traverse the array
for (int i = 0; i < N; ++i) {
// If number is perfect
if (isPerfect(arr[i])) {
// Stores sum of digits
// of the number
int sum = sumOfDigits(arr[i]);
// If that is also perfect number
if (isPerfect(sum)) {
// Print that number
System.out.print(arr[i] + " ");
}
}
}
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 3, 8, 12, 28, 6 };
// Size of the array
int N = arr.length;
// Function call to count perfect numbers
// having sum of digits also perfect
countPerfectNumbers(arr, N);
}
}
// This code is contributed by Kingash.
Python3
# Python Program to implement
# the above approach
# Function to check if a number
# is perfect number or not
def isPerfect(N):
# Stores sum of proper divisors
sumOfDivisors = 1;
for i in range(2, int(N / 2) + 1):
if (N % i == 0):
sumOfDivisors += i;
# If sum of digits is equal to N,
# then it's a perfect number
if (sumOfDivisors == N):
return True;
# Otherwise, not a perfect number
else:
return False;
# Function to find the
# sum of digits of a number
def sumOfDigits(N):
# Stores sum of digits
sum = 0;
while (N != 0):
sum += (N % 10);
N = N // 10;
# Return sum of digits
return sum;
# Function to count perfect numbers from
# an array whose sum of digits is also perfect
def countPerfectNumbers(arr, N):
# Traverse the array
for i in range(N):
# If number is perfect
if (isPerfect(arr[i])):
# Stores sum of digits
# of the number
sum = sumOfDigits(arr[i]);
# If that is also perfect number
if (isPerfect(sum)):
# Print that number
print(arr[i], end=" ");
# Driver Code
if __name__ == '__main__':
# Given array
arr = [3, 8, 12, 28, 6];
# Size of the array
N = len(arr);
# Function call to count perfect numbers
# having sum of digits also perfect
countPerfectNumbers(arr, N);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if a number
// is perfect number or not
static bool isPerfect(int N)
{
// Stores sum of proper divisors
int sumOfDivisors = 1;
for (int i = 2; i <= N / 2; ++i) {
if (N % i == 0) {
sumOfDivisors += i;
}
}
// If sum of digits is equal to N,
// then it's a perfect number
if (sumOfDivisors == N) {
return true;
}
// Otherwise, not a perfect number
else
return false;
}
// Function to find the
// sum of digits of a number
static int sumOfDigits(int N)
{
// Stores sum of digits
int sum = 0;
while (N != 0) {
sum += (N % 10);
N = N / 10;
}
// Return sum of digits
return sum;
}
// Function to count perfect numbers from
// an array whose sum of digits is also perfect
static void countPerfectNumbers(int []arr, int N)
{
// Traverse the array
for (int i = 0; i < N; ++i) {
// If number is perfect
if (isPerfect(arr[i])) {
// Stores sum of digits
// of the number
int sum = sumOfDigits(arr[i]);
// If that is also perfect number
if (isPerfect(sum)) {
// Print that number
Console.Write(arr[i] + " ");
}
}
}
}
// Driver Code
static public void Main()
{
// Given array
int []arr = { 3, 8, 12, 28, 6 };
// Size of the array
int N = arr.Length;
// Function call to count perfect numbers
// having sum of digits also perfect
countPerfectNumbers(arr, N);
}
}
// This code is contributed by jana_sayantan.
JavaScript
<script>
// JavaScript program for the above approach
// Function to check if a number
// is perfect number or not
function isPerfect(N)
{
// Stores sum of proper divisors
let sumOfDivisors = 1;
for (let i = 2; i <= Math.floor(N / 2); ++i)
{
if (N % i === 0)
{
sumOfDivisors += i;
}
}
// If sum of digits is equal to N,
// then it's a perfect number
if (sumOfDivisors === N) {
return 1;
}
// Otherwise, not a perfect number
else
return 0;
}
// Function to find the
// sum of digits of a number
function sumOfDigits(N)
{
// Stores sum of digits
let sum = 0;
while (N != 0) {
sum += (N % 10);
N = Math.floor(N / 10);
}
// Return sum of digits
return sum;
}
// Function to count perfect numbers from
// an array whose sum of digits is also perfect
function countPerfectNumbers(arr, N)
{
// Traverse the array
for (let i = 0; i < N; ++i) {
// If number is perfect
if (isPerfect(arr[i])) {
// Stores sum of digits
// of the number
let sum = sumOfDigits(arr[i]);
// If that is also perfect number
if (isPerfect(sum)) {
// Print that number
document.write(arr[i] + " ");
}
}
}
}
// Driver Code
// Given array
let arr = [ 3, 8, 12, 28, 6 ];
// Size of the array
let N = arr.length;
// Function call to count perfect numbers
// having sum of digits also perfect
countPerfectNumbers(arr, N);
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(N3 * log N)
Auxiliary Space: O(1)
Similar Reads
Print prime numbers with prime sum of digits in an array Given an array arr[] and the task is to print the additive primes in an array. Additive primes: Primes such that the sum of their digits is also a prime, such as 2, 3, 7, 11, 23 are additive primes but not 13, 19, 31 etc.Examples: Input: arr[] = {2, 4, 6, 11, 12, 18, 7} Output: 2, 11, 7 Input: arr[]
8 min read
Sum of all subsets whose sum is a Perfect Number from a given array Given an array arr[] consisting of N integers, the task is to find the sum of all subsets from an array, whose sum is a Perfect Number. Examples: Input: arr[] = {5, 4, 6}Output: 6Explanation:All possible subsets from the array arr[] are:{5} ? Sum = 5{4} ? Sum = 4.{6} ? Sum = 6.{5, 4} ? Sum = 9.{5, 6
14 min read
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 it 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
5 min read
Minimum sum of two numbers formed from digits of an array in O(n) Given an array of digits (values are from 0 to 9), find the minimum possible sum of two numbers formed from digits of the array. All digits of the given array must be used to form the two numbers.Examples: Input: arr[] = {6, 8, 4, 5, 2, 3} Output: 604 246 + 358 = 604Input: arr[] = {5, 3, 0, 7, 4} Ou
6 min read
Sum of all numbers that can be formed with permutations of n digits Given n distinct digits (from 0 to 9), find sum of all n digit numbers that can be formed using these digits. It is assumed that numbers formed with leading 0 are allowed. Example: Input: 1 2 3 Output: 1332 Explanation Numbers Formed: 123 , 132 , 312 , 213, 231 , 321 123 + 132 + 312 + 213 + 231 + 32
8 min read