📜  完美数字不变数

📅  最后修改于: 2021-05-04 18:45:41             🧑  作者: Mango

一个正整数,如果其数字的某些固定幂的总和等于数字本身,则称为完美数字不变数
对于任何数字, abcd…= pow(a,n)+ pow(b,n)+ pow(c,n)+ pow(d,n)+…。
其中n可以是大于0的任何整数。

检查N是否为完美数字不变数

给定数字N ,任务是检查给定数字N是否为Perfect Digital Invariant Number 。如果N是一个完美的数字不变数,则打印“是”,否则打印“否”
例子:

方法:对于数字N中的每个数字,从固定数字1开始计算其数字幂的总和,直到N的数字幂之和超过N为止。如果N是一个完美的数字不变数,则打印“是”,否则打印“否”

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate x raised
// to the power y
int power(int x, unsigned int y)
{
    if (y == 0) {
        return 1;
    }
 
    if (y % 2 == 0) {
        return (power(x, y / 2)
                * power(x, y / 2));
    }
    return (x
            * power(x, y / 2)
            * power(x, y / 2));
}
 
// Function to check whether the given
// number is Perfect Digital Invariant
// number or not
bool isPerfectDigitalInvariant(int x)
{
    for (int fixed_power = 1;; fixed_power++) {
 
        int temp = x, sum = 0;
 
        // For each digit in temp
        while (temp) {
 
            int r = temp % 10;
            sum += power(r, fixed_power);
            temp = temp / 10;
        }
 
        // If satisfies Perfect Digital
        // Invariant condition
        if (sum == x) {
            return true;
        }
        // If sum exceeds n, then not possible
        if (sum > x) {
            return false;
        }
    }
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 4150;
 
    // Function Call
    if (isPerfectDigitalInvariant(N))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to calculate x raised
// to the power y
static int power(int x, int y)
{
    if (y == 0)
    {
        return 1;
    }
  
    if (y % 2 == 0)
    {
        return (power(x, y / 2) *
                power(x, y / 2));
    }
    return (x * power(x, y / 2) *
                power(x, y / 2));
}
  
// Function to check whether the given
// number is Perfect Digital Invariant
// number or not
static boolean isPerfectDigitalInvariant(int x)
{
    for (int fixed_power = 1;; fixed_power++)
    {
        int temp = x, sum = 0;
  
        // For each digit in temp
        while (temp > 0)
        {
            int r = temp % 10;
            sum += power(r, fixed_power);
            temp = temp / 10;
        }
  
        // If satisfies Perfect Digital
        // Invariant condition
        if (sum == x)
        {
            return true;
        }
        // If sum exceeds n, then not possible
        if (sum > x)
        {
            return false;
        }
    }
}
  
// Driver Code
public static void main(String[] args)
{
    // Given Number N
    int N = 4150;
  
    // Function Call
    if (isPerfectDigitalInvariant(N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 implementation of the above approach
 
# Function to find the
# sum of divisors
def power(x, y):
    if (y == 0):
        return 1
    if (y % 2 == 0):
        return (power(x, y//2) * power(x, y//2))
    return (x * power(x, y//2) * power(x, y//2))
     
 
# Function to check whether the given
# number is Perfect Digital Invariant
# number or not
def isPerfectDigitalInvariant(x):
    fixed_power = 0
    while True:
        fixed_power += 1
        temp = x
        summ = 0
 
        # For each digit in temp
        while (temp):
            r = temp % 10
            summ = summ + power(r, fixed_power)
            temp = temp//10
 
        # If satisfies Perfect Digital
        # Invariant condition
        if (summ == x):
            return (True)
           
        # If sum exceeds n, then not possible
        if (summ > x):
            return (False)
 
# Driver code
# Given Number N
N = 4150
 
# Function Call
if (isPerfectDigitalInvariant(N)):
    print("Yes")
else:
    print("No")
 
    # This code is contributed by vikas_g


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate x raised
// to the power y
static int power(int x, int y)
{
    if (y == 0)
    {
        return 1;
    }
 
    if (y % 2 == 0)
    {
        return (power(x, y / 2) *
                power(x, y / 2));
    }
    return (x * power(x, y / 2) *
                power(x, y / 2));
}
 
// Function to check whether the given
// number is Perfect Digital Invariant
// number or not
static bool isPerfectDigitalInvariant(int x)
{
    for(int fixed_power = 1;; fixed_power++)
    {
        int temp = x, sum = 0;
 
        // For each digit in temp
        while (temp > 0)
        {
            int r = temp % 10;
            sum += power(r, fixed_power);
            temp = temp / 10;
        }
 
        // If satisfies Perfect Digital
        // Invariant condition
        if (sum == x)
        {
            return true;
        }
        // If sum exceeds n, then not possible
        if (sum > x)
        {
            return false;
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number N
    int N = 4150;
 
    // Function call
    if (isPerfectDigitalInvariant(N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by PrinciRaj1992


输出:
Yes