Deserium Number

Deserium Number: A number is said to be Deserium number if the sum of the digits of a number with respect to the power of from 1 to the number of digits is equal to the number itself is known as Deserium Number.

Examples :

Input : 135
Output : Yes
1^1 + 3^2 + 5^3 = 135

Input : 9
Output : Yes
9^1 = 9

Input  : 125
Output : No
1^1+2^2+5^3 != 125

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is simple.
1) Count digits in given number.
2) Traverse from rightmost digit to leftmost and compute sum of powers.
3) If sum of powers is equal to given number, return true.

C++

 // C++ program to check whether a number // is Deserium number or not #include #include    using namespace std;    // Returns count of digits in n. int countDigits(int n) {     int c = 0;            do {         c++;         n = n / 10;     } while (n != 0);            return c; }    // Returns true if x is Diserium bool isDeserium(int x) {     int temp = x;     int p = countDigits(x);        // Compute powers of digits     // from right to left.     int sum = 0;     while (x != 0) {         int digit = x % 10;         sum += pow(digit, p);         p--;         x = x / 10;     }        // If sum of powers is same as     // given number.     return (sum == temp); }    // Driver code int main() {     int x = 135;            if (isDeserium(x))         cout << "Yes";     else         cout << "No";                return 0; }    // This code is contributed by vt_m.

Java

 // Java program to check whether a number // is Deserium number or not import java.util.Scanner; class Deserium {        // Returns count of digits in n.     static int countDigits(int n)     {         int c = 0;         do {             c++;             n = n / 10;         } while (n != 0);         return c;     }        // Returns true if x is Diserium     static boolean isDeserium(int x)     {         int temp = x;         int p = countDigits(x);            // Compute powers of digits          // from right to left.         int sum = 0;         while (x != 0) {             int digit = x % 10;             sum += Math.pow(digit, p);             p--;             x = x / 10;         }            // If sum of powers is same as         // given number.         return (sum == temp);     }        // Driver code     public static void main(String[] args)     {         int x = 135;         if (isDeserium(x))             System.out.println("Yes");         else             System.out.println("No");     } }

Python3

 # Python3 program to check whether  # a number is Deserium number or not    # Returns count of digits in n. def countDigits(n):        c = 0     while (n != 0):         c += 1         n = int( n / 10)            return c    # Returns true if x is Diserium def isDeserium(x):        temp = x     p = countDigits(x)        # Compute powers of digits     # from right to left.     sum = 0     while (x != 0):          digit = int(x % 10)         sum += pow(digit, p)         p -= 1         x = int(x / 10)               # If sum of powers is same as     # given number.     return (sum == temp)    # Driver code x = 135 if (isDeserium(x)):     print("Yes") else:     print("No")            # This code is contributed by Smitha Dinesh Semwal.

C#

 // C# program to check whether a number // is Deserium number or not using System;    class Deserium {        // Returns count of digits in n.     static int countDigits(int n)     {         int c = 0;         do {             c++;             n = n / 10;         } while (n != 0);         return c;     }        // Returns true if x is Diserium     static bool isDeserium(int x)     {         int temp = x;         int p = countDigits(x);            // Compute powers of digits         // from right to left.         int sum = 0;         while (x != 0) {             int digit = x % 10;             sum += (int)Math.Pow(digit, p);             p--;             x = x / 10;         }            // If sum of powers is same as         // given number.         return (sum == temp);     }        // Driver code     public static void Main()     {         int x = 135;                    if (isDeserium(x))             Console.WriteLine("Yes");         else             Console.WriteLine("No");     } }    // This code is contributed by vt_m.

PHP



Output :

Yes

My Personal Notes arrow_drop_up Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator I just love Programming languages and love to know new concepts everyday,every minute,every second Here to help Other GEEKS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.