Skip to content
Related Articles

Related Articles

Perfect Digital Invariants number
  • Last Updated : 04 Mar, 2021

A positive integer is called a Perfect Digital Invariant Number if the sum of some fixed power of their digits is equal to the number itself. 
For any number, abcd… = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n) + ….
where n can be any integer greater than 0.
 

Check if N is Perfect Digital Invariant Number or not

Given a number N, the task is to check if the given number N is Perfect Digital Invariant Number or not. If N is a Perfect Digital Invariant Number then print “Yes” else print “No”.
Example: 
 

Input: N = 153 
Output: Yes 
Explanation: 
153 is a Perfect Digital Invariants number as for n = 3 we have 
13 + 53 + 33 = 153
Input: 4150 
Output: Yes 
Explanation: 
4150 is a Perfect Digital Invariants number as for n = 5 we have 
45 + 15 + 55 + 05 = 4150 
 

 

Approach: For every digit in number N, calculate the sum of its digit power starting from a fixed number 1 until the sum of digit’s power of N exceeds N. If N is a Perfect Digital Invariant Number then print “Yes” else print “No”.
 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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
Output: 
Yes

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :