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 |
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.