Program to check Plus Perfect Number
Given a ‘n’ digit number x, check if it is a plus-perfect number or not. A number is plus perfect number if it is equal to the sum of its digits raised to the nth power.
Examples :
Input : x = 371
Output : Yes
Explanation :
Number of digits n = 3
(3*3*3) + (7*7*7) + (1*1*1) = 371
Input : x = 9474
Output : Yes
Explanation :
Number of digits n = 4
(9*9*9*9) + (4*4*4*4) + (7*7*7*7) +
(4*4*4*4) = 9474
Input : x = 9473
Output : No
Explanation :
Number of digits n = 4
(9*9*9*9) + (4*4*4*4) + (7*7*7*7) +
(3*3*3*3) != 9474
Below is the implementation to check if a number is plus perfect number or not.
C++
// CPP implementation to check // if the number is plus perfect // or not #include <bits/stdc++.h> using namespace std; // function to check plus perfect number bool checkplusperfect( int x) { int temp = x; // calculating number of digits int n = 0; while (x != 0) { x /= 10; n++; } // calculating plus perfect number x = temp; int sum = 0; while (x != 0) { sum += pow (x % 10, n); x /= 10; } // checking whether number // is plus perfect or not return (sum == temp); } // driver program int main() { int x = 9474; if (checkplusperfect(x)) cout << "Yes" ; else cout << "No" ; return 0; } |
Java
// java implementation to check // if the number is plus perfect // or not import java.io.*; class GFG { // function to check plus perfect number static boolean checkplusperfect( int x) { int temp = x; // calculating number of digits int n = 0 ; while (x != 0 ) { x /= 10 ; n++; } // calculating plus perfect number x = temp; int sum = 0 ; while (x != 0 ) { sum += Math.pow(x % 10 , n); x /= 10 ; } // checking whether number // is plus perfect or not return (sum == temp); } // Driver program public static void main (String[] args) { int x = 9474 ; if (checkplusperfect(x)) System.out.println ( "Yes" ); else System.out.println ( "No" ); } } // This code is contributed by vt_m |
Python3
# Python 3 implementation to check # if the number is plus perfect # or not import math # function to check plus perfect number def checkplusperfect(x) : temp = x # calculating number of digits n = 0 while (x ! = 0 ) : x = x / / 10 n = n + 1 # calculating plus perfect number x = temp sm = 0 while (x ! = 0 ) : sm = sm + ( int )(math. pow (x % 10 , n)) x = x / / 10 # checking whether number # is plus perfect or not return (sm = = temp) # driver program x = 9474 if (checkplusperfect(x)) : print ( "Yes" ) else : print ( "No" ) # This code is contributed by Nikita Tiwari. |
C#
// C# implementation to check // if the number is plus perfect // or not using System; class GFG { // function to check plus perfect number static bool checkplusperfect( int x) { int temp = x; // calculating number of digits int n = 0; while (x != 0) { x /= 10; n++; } // calculating plus perfect number x = temp; int sum = 0; while (x != 0) { sum += ( int )Math.Pow(x % 10, n); x /= 10; } // checking whether number // is plus perfect or not return (sum == temp); } // Driver program public static void Main () { int x = 9474; if (checkplusperfect(x)) Console.WriteLine ( "Yes" ); else Console.WriteLine ( "No" ); } } // This code is contributed by vt_m |
Javascript
<script> // Javascript implementation to check // if the number is plus perfect // or not // Function to check plus perfect number function checkplusperfect(x) { let temp = x; // Calculating number of digits let n = 0; while (x != 0) { x = parseInt(x / 10); n++; } // Calculating plus perfect number x = temp; let sum = 0; while (x != 0) { sum += Math.pow(x % 10, n); x = parseInt(x/10); } // Checking whether number // is plus perfect or not return (sum == temp); } // Driver Code let x = 9474; if (checkplusperfect(x)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by souravmahato348 </script> |
PHP
<?php // PHP implementation to check // if the number is plus perfect // or not // function to check plus // perfect number function checkplusperfect( $x ) { $temp = $x ; // calculating number // of digits $n = 0; while ( $x != 0) { $x /= 10; $n ++; } // calculating plus // perfect number $x = $temp ; $sum = 0; while ( $x != 0) { $sum += pow( $x % 10, $n ); $x /= 10; } // checking whether number // is plus perfect or not return ( $sum == $temp ); } // Driver Code $x = 9474; if (checkplusperfect(! $x )) echo "Yes" ; else echo "No" ; // This code is contributed by ajit ?> |
Output
Yes
Time complexity: O(log10(x))
Auxiliary space: O(1)
Below is the implementation to check if a number is plus perfect number or not.
Approach:
- The function works by first finding all the factors of the given number using a for loop. We then check if the sum of factors is equal to the given number. If it is, then we return True.In this program, we define a function called is_plus_perfect that takes a number as input and returns True if the number is a Plus Perfect Number, and False otherwise.
- If the sum of factors is not equal to the given number, we check if the sum of factors of the sum of factors (excluding 1) is equal to the given number. If it is, then we return True. If neither condition is met, then we return False.
- Finally, we call the is_plus_perfect function with a sample number of 28 and print whether it is a Plus Perfect Number or not using an if-else statement.
C++
#include <iostream> #include <vector> using namespace std; // Plus Perfect Number Checker Function bool isPlusPerfect( int num) { // Finding factors of the number vector< int > factors; for ( int i = 1; i < num; i++) { if (num % i == 0) { factors.push_back(i); } } // Checking if the sum of factors is equal to the // number int factorSum = 0; for ( int factor : factors) { factorSum += factor; } if (factorSum == num) { return true ; } // Checking if the sum of factors of the sum of // factors is equal to the number int secondFactorSum = 0; for ( int factor : factors) { if (factor != 1) { vector< int > secondFactors; for ( int i = 1; i < factor; i++) { if (factor % i == 0) { secondFactors.push_back(i); } } for ( int secondFactor : secondFactors) { secondFactorSum += secondFactor; } } } if (secondFactorSum == num) { return true ; } return false ; } int main() { // Sample Input int number = 28; // Checking if the number is a Plus Perfect Number if (isPlusPerfect(number)) { cout << number << " is a Plus Perfect Number" << endl; } else { cout << number << " is not a Plus Perfect Number" << endl; } return 0; } |
Java
import java.util.*; public class GFG { // Plus Perfect Number Checker Function public static boolean isPlusPerfect( int num) { // Finding factors of the number List<Integer> factors = new ArrayList<Integer>(); for ( int i = 1 ; i < num; i++) { if (num % i == 0 ) { factors.add(i); } } // Checking if the sum of factors is equal to the // number int factorSum = 0 ; for ( int factor : factors) { factorSum += factor; } if (factorSum == num) { return true ; } // Checking if the sum of factors of the sum of // factors is equal to the number int secondFactorSum = 0 ; for ( int factor : factors) { if (factor != 1 ) { List<Integer> secondFactors = new ArrayList<Integer>(); for ( int i = 1 ; i < factor; i++) { if (factor % i == 0 ) { secondFactors.add(i); } } for ( int secondFactor : secondFactors) { secondFactorSum += secondFactor; } } } if (secondFactorSum == num) { return true ; } return false ; } public static void main(String[] args) { // Sample Input int number = 28 ; // Checking if the number is a Plus Perfect Number if (isPlusPerfect(number)) { System.out.println( number + " is a Plus Perfect Number" ); } else { System.out.println( number + " is not a Plus Perfect Number" ); } } } |
Python3
# Plus Perfect Number Checker Function def is_plus_perfect(num): # Finding factors of the number factors = [] for i in range ( 1 , num): if num % i = = 0 : factors.append(i) # Checking if the sum of factors is equal to the number factor_sum = sum (factors) if factor_sum = = num: return True # Checking if the sum of factors of the sum of factors is equal to the number second_factor_sum = sum ([i for i in factors if i ! = 1 ]) if second_factor_sum = = num: return True return False # Sample Input number = 28 # Checking if the number is a Plus Perfect Number if is_plus_perfect(number): print (f "{number} is a Plus Perfect Number" ) else : print (f "{number} is not a Plus Perfect Number" ) |
C#
using System; using System.Collections.Generic; class Gfg { // Plus Perfect Number Checker Function static bool isPlusPerfect( int num) { // Finding factors of the number List< int > factors = new List< int >(); for ( int i = 1; i < num; i++) { if (num % i == 0) { factors.Add(i); } } // Checking if the sum of factors is equal to the number int factorSum = 0; foreach ( int factor in factors) { factorSum += factor; } if (factorSum == num) { return true ; } // Checking if the sum of factors of the sum of factors // is equal to the number int secondFactorSum = 0; foreach ( int factor in factors) { if (factor != 1) { List< int > secondFactors = new List< int >(); for ( int i = 1; i < factor; i++) { if (factor % i == 0) { secondFactors.Add(i); } } foreach ( int secondFactor in secondFactors) { secondFactorSum += secondFactor; } } } if (secondFactorSum == num) { return true ; } return false ; } static void Main( string [] args) { // Sample Input int number = 28; // Checking if the number is a Plus Perfect Number if (isPlusPerfect(number)) { Console.WriteLine(number + " is a Plus Perfect Number" ); } else { Console.WriteLine(number + " is not a Plus Perfect Number" ); } } } |
Javascript
// Plus Perfect Number Checker Function function isPlusPerfect(num) { // Finding factors of the number const factors = []; for (let i = 1; i < num; i++) { if (num % i === 0) { factors.push(i); } } // Checking if the sum of factors is equal to the number const factorSum = factors.reduce((sum, factor) => sum + factor, 0); if (factorSum === num) { return true ; } // Checking if the sum of factors of the sum of factors is equal to the number const secondFactorSum = factors.filter(factor => factor !== 1) .reduce((sum, factor) => sum + factor, 0); if (secondFactorSum === num) { return true ; } return false ; } // Sample Input const number = 28; // Checking if the number is a Plus Perfect Number if (isPlusPerfect(number)) { console.log(`${number} is a Plus Perfect Number`); } else { console.log(`${number} is not a Plus Perfect Number`); } |
Output
28 is a Plus Perfect Number
Time complexity :O(n)
space complexity : O(n)
Contact Us