Número de Deserium: Se dice que un número es Número de Deserium si la suma de los dígitos de un número con respecto a la potencia de 1 al número de dígitos es igual al número en sí se conoce como Número de Deserium.
Ejemplos:
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
La idea es sencilla.
1) Contar dígitos en un número dado.
2) Recorra desde el dígito más a la derecha hasta el más a la izquierda y calcule la suma de las potencias.
3) Si la suma de las potencias es igual al número dado, devuelve verdadero.
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>
Producción :
Yes
Publicación traducida automáticamente
Artículo escrito por Bishal Kumar Dubey y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA