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++
#include <bits/stdc++.h>
using namespace std;
bool checkplusperfect( int x)
{
int temp = x;
int n = 0;
while (x != 0) {
x /= 10;
n++;
}
x = temp;
int sum = 0;
while (x != 0) {
sum += pow (x % 10, n);
x /= 10;
}
return (sum == temp);
}
int main()
{
int x = 9474;
if (checkplusperfect(x))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkplusperfect( int x)
{
int temp = x;
int n = 0 ;
while (x != 0 )
{
x /= 10 ;
n++;
}
x = temp;
int sum = 0 ;
while (x != 0 )
{
sum += Math.pow(x % 10 , n);
x /= 10 ;
}
return (sum == temp);
}
public static void main (String[] args)
{
int x = 9474 ;
if (checkplusperfect(x))
System.out.println ( "Yes" );
else
System.out.println ( "No" );
}
}
|
Python3
import math
def checkplusperfect(x) :
temp = x
n = 0
while (x ! = 0 ) :
x = x / / 10
n = n + 1
x = temp
sm = 0
while (x ! = 0 ) :
sm = sm + ( int )(math. pow (x % 10 , n))
x = x / / 10
return (sm = = temp)
x = 9474
if (checkplusperfect(x)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool checkplusperfect( int x)
{
int temp = x;
int n = 0;
while (x != 0)
{
x /= 10;
n++;
}
x = temp;
int sum = 0;
while (x != 0)
{
sum += ( int )Math.Pow(x % 10, n);
x /= 10;
}
return (sum == temp);
}
public static void Main ()
{
int x = 9474;
if (checkplusperfect(x))
Console.WriteLine ( "Yes" );
else
Console.WriteLine ( "No" );
}
}
|
PHP
<?php
function checkplusperfect( $x )
{
$temp = $x ;
$n = 0;
while ( $x != 0)
{
$x /= 10;
$n ++;
}
$x = $temp ;
$sum = 0;
while ( $x != 0)
{
$sum += pow( $x % 10, $n );
$x /= 10;
}
return ( $sum == $temp );
}
$x = 9474;
if (checkplusperfect(! $x ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkplusperfect(x)
{
let temp = x;
let n = 0;
while (x != 0)
{
x = parseInt(x / 10);
n++;
}
x = temp;
let sum = 0;
while (x != 0)
{
sum += Math.pow(x % 10, n);
x = parseInt(x/10);
}
return (sum == temp);
}
let x = 9474;
if (checkplusperfect(x))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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;
bool isPlusPerfect( int num)
{
vector< int > factors;
for ( int i = 1; i < num; i++) {
if (num % i == 0) {
factors.push_back(i);
}
}
int factorSum = 0;
for ( int factor : factors) {
factorSum += factor;
}
if (factorSum == num) {
return true ;
}
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()
{
int number = 28;
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 {
public static boolean isPlusPerfect( int num)
{
List<Integer> factors = new ArrayList<Integer>();
for ( int i = 1 ; i < num; i++) {
if (num % i == 0 ) {
factors.add(i);
}
}
int factorSum = 0 ;
for ( int factor : factors) {
factorSum += factor;
}
if (factorSum == num) {
return true ;
}
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)
{
int number = 28 ;
if (isPlusPerfect(number)) {
System.out.println(
number + " is a Plus Perfect Number" );
}
else {
System.out.println(
number + " is not a Plus Perfect Number" );
}
}
}
|
Python3
def is_plus_perfect(num):
factors = []
for i in range ( 1 , num):
if num % i = = 0 :
factors.append(i)
factor_sum = sum (factors)
if factor_sum = = num:
return True
second_factor_sum = sum ([i for i in factors if i ! = 1 ])
if second_factor_sum = = num:
return True
return False
number = 28
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
{
static bool isPlusPerfect( int num)
{
List< int > factors = new List< int >();
for ( int i = 1; i < num; i++)
{
if (num % i == 0)
{
factors.Add(i);
}
}
int factorSum = 0;
foreach ( int factor in factors)
{
factorSum += factor;
}
if (factorSum == num)
{
return true ;
}
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)
{
int number = 28;
if (isPlusPerfect(number))
{
Console.WriteLine(number + " is a Plus Perfect Number" );
}
else
{
Console.WriteLine(number + " is not a Plus Perfect Number" );
}
}
}
|
Javascript
function isPlusPerfect(num) {
const factors = [];
for (let i = 1; i < num; i++) {
if (num % i === 0) {
factors.push(i);
}
}
const factorSum = factors.reduce((sum, factor) => sum + factor, 0);
if (factorSum === num) {
return true ;
}
const secondFactorSum = factors.filter(factor => factor !== 1)
.reduce((sum, factor) => sum + factor, 0);
if (secondFactorSum === num) {
return true ;
}
return false ;
}
const number = 28;
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)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!