Un número de Katadrome es un número cuyos dígitos están en orden decreciente.
Pocos números de Katadrome son:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21….
Comprobar si N es un Katadromes
Dado un número N , la tarea es comprobar si es Katadromes o no.
Ejemplos:
Entrada: 4321
Salida: Sí
Entrada: 1243
Salida: No
Planteamiento: La idea es recorrer los dígitos del número y verificar si el dígito actual es menor que el último dígito. Si todos los dígitos cumplen las condiciones, entonces el número es un número de Katadrome.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check if // a number is Katadrome or not. #include <iostream> using namespace std; // Function to check if a number // is a Katadrome number or not bool isKatadrome(int num) { // To store previous digit (Assigning // initial value which is less than any // digit) int prev = -1; // Traverse all digits from right to // left and check if any digit is // smaller than previous. while (num) { int digit = num % 10; num /= 10; if (digit < prev) return false; prev = digit; } return true; } // Driver code int main() { int num = 4321; isKatadrome(num) ? cout << "Yes" : cout << "No"; return 0; }
Java
// Java implementation to check if // a number is Katadrome or not. class GFG{ // Function to check if a number // is a Katadrome number or not static boolean isKatadrome(int num) { // To store previous digit // (Assigning initial value // which is less than any digit) int prev = -1; // Traverse all digits from right // to left and check if any digit // is smaller than previous. while (num > 0) { int digit = num % 10; num /= 10; if (digit < prev) return false; prev = digit; } return true; } // Driver Code public static void main(String[] args) { int N = 4321; // Function Call if (isKatadrome(N)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Pratima Pandey
Python3
# Python3 program to print count of values such # that n+i = n^i def isKatadrome(num): # To store previous digit (Assigning # initial value which is less than any # digit) prev = -1 # Traverse all digits from right to # left and check if any digit is # smaller than previous. while num: digit = num % 10 num //= 10 if digit < prev: return False prev = digit return True # Driver code if __name__=='__main__': num = 4321 if isKatadrome(num): print('Yes') else: print('No') # This code is contributed by rutvik
C#
// C# implementation to check if // a number is Katadrome or not. using System; class GFG{ // Function to check if a number // is a Katadrome number or not static bool isKatadrome(int num) { // To store previous digit // (Assigning initial value // which is less than any digit) int prev = -1; // Traverse all digits from right // to left and check if any digit // is smaller than previous. while (num > 0) { int digit = num % 10; num /= 10; if (digit < prev) return false; prev = digit; } return true; } // Driver Code public static void Main() { int N = 4321; // Function Call if (isKatadrome(N)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript implementation to check if // a number is Katadrome or not. // Function to check if a number // is a Katadrome number or not function isKatadrome( num) { // To store previous digit // (Assigning initial value // which is less than any digit) let prev = -1; // Traverse all digits from right // to left and check if any digit // is smaller than previous. while (num > 0) { let digit = num % 10; num = parseInt(num/10); if (digit < prev) return false; prev = digit; } return true; } // Driver Code let N = 4321; // Function Call if (isKatadrome(N)) document.write("Yes"); else document.write("No"); // This code contributed by Rajput-Ji </script>
Producción:
Yes
Complejidad de tiempo: O(d) donde d es el número de dígitos en un número dado.
Referencia: http://www.numbersaplenty.com/set/katadrome/