📜  程序检查Plus完美数字

📅  最后修改于: 2021-04-29 13:29:28             🧑  作者: Mango

给定一个‘n’数字x,请检查它是否为正整数。如果数字等于加到n次幂的数字的总和,则该数字为正数。

例子 :

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

下面是检查数字是否为正数的实现。

C++
// CPP implementation to check
// if the number is plus perfect
// or not
#include 
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


PHP


Javascript


输出 :

Yes