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
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 <iostream> #include <math.h> 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 Deserium 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 Deserium 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 Deserium 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 Deserium 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
<?php // PHP program to check // whether a number is // Deserium number or not // Returns count of // digits in n. function countDigits( $n ) { $c = 0; do { $c ++; $n = $n / 10; } while ( $n != 0); return $c ; } // Returns true if // x is Deserium function isDeserium( $x ) { $temp = $x ; $p = countDigits( $x ); // Compute powers of digits // from right to left. $sum = 0; while ( $x != 0) { $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 $x = 135; if (isDeserium( $x )) echo "No" ; else echo "Yes" ; // This code is contributed by aj_36. ?> |
Javascript
<script> // JavaScript program to check whether // a number is Deserium number or not // Returns count of digits in n. function countDigits(n) { let c = 0; do { c++; n = Math.floor(n / 10); } while (n != 0); return c; } // Returns true if x is Deserium function isDeserium(x) { let temp = x; let p = countDigits(x); // Compute powers of digits // from right to left. let sum = 0; while (x != 0) { let digit = x % 10; sum += Math.floor(Math.pow(digit, p)); p--; x = Math.floor(x / 10); } // If sum of powers is same as // given number. return (sum == temp); } // Driver Code let x = 135; if (isDeserium(x)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by code_hunt </script> |
Output :
Yes
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!