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++ 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 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 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# 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 |
<script> // Javascript implementation // Function to calculate x raised // to the power y function power(x, y)
{ if (y == 0) {
return 1;
}
if (y % 2 == 0) {
return (power(x, Math.floor(y / 2))
* power(x, Math.floor(y / 2)));
}
return (x
* power(x, Math.floor(y / 2))
* power(x, Math.floor(y / 2)));
} // Function to check whether the given // number is Perfect Digital Invariant // number or not function isPerfectDigitalInvariant(x)
{ for ( var fixed_power = 1;; fixed_power++) {
var temp = x, sum = 0;
// For each digit in temp
while (temp) {
var r = temp % 10;
sum += power(r, fixed_power);
temp = Math.floor(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 // Given Number N var N = 4150;
// Function Call if (isPerfectDigitalInvariant(N))
document.write( "Yes" );
else document.write( "No" );
// This code is contributed by shubhamsingh10 </script> |
Yes