Dado un número entero N , la tarea es comprobar si N es una factoría o no. Un factorio es un número que es igual a la suma de los factoriales de sus dígitos.
Ejemplos:
Entrada: N = 40585
Salida: Sí
4! + 0! + 5! + 8! + 5! = 40585
Entrada: N = 234
Salida: No
2! + 3! + 4! = 32
Enfoque: Cree una array fact[] de tamaño 10 para almacenar los factoriales de todos los dígitos posibles donde fact[i] almacena i. . Ahora, para todos los dígitos del número dado, encuentre la suma de los factoriales de los dígitos usando la array fact[] calculada anteriormente. Si la suma es igual al número dado, entonces el número es una factoría, de lo contrario no lo es.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define MAX 10 // Function that returns true // if n is a Factorion bool isFactorion(int n) { // fact[i] will store i! int fact[MAX]; fact[0] = 1; for (int i = 1; i < MAX; i++) fact[i] = i * fact[i - 1]; // A copy of the given integer int org = n; // To store the sum of factorials // of the digits of n int sum = 0; while (n > 0) { // Get the last digit int d = n % 10; // Add the factorial of the current // digit to the sum sum += fact[d]; // Remove the last digit n /= 10; } if (sum == org) return true; return false; } // Driver code int main() { int n = 40585; if (isFactorion(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the above approach class GFG { static int MAX = 10; // Function that returns true // if n is a Factorion static boolean isFactorion(int n) { // fact[i] will store i! int fact[] = new int[MAX]; fact[0] = 1; for (int i = 1; i < MAX; i++) fact[i] = i * fact[i - 1]; // A copy of the given integer int org = n; // To store the sum of factorials // of the digits of n int sum = 0; while (n > 0) { // Get the last digit int d = n % 10; // Add the factorial of the current // digit to the sum sum += fact[d]; // Remove the last digit n /= 10; } if (sum == org) return true; return false; } // Driver code public static void main (String[] args) { int n = 40585; if (isFactorion(n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach MAX = 10 # Function that returns true # if n is a Factorion def isFactorion(n) : # fact[i] will store i! fact = [0] * MAX fact[0] = 1 for i in range(1, MAX) : fact[i] = i * fact[i - 1] # A copy of the given integer org = n # To store the sum of factorials # of the digits of n sum = 0 while (n > 0) : # Get the last digit d = n % 10 # Add the factorial of the current # digit to the sum sum += fact[d] # Remove the last digit n = n // 10 if (sum == org): return True return False # Driver code n = 40585 if (isFactorion(n)): print("Yes") else: print("No") # This code is contributed by # divyamohan123
C#
// C# implementation of the above approach using System; class GFG { static int MAX = 10; // Function that returns true // if n is a Factorion static bool isFactorion(int n) { // fact[i] will store i! int [] fact = new int[MAX]; fact[0] = 1; for (int i = 1; i < MAX; i++) fact[i] = i * fact[i - 1]; // A copy of the given integer int org = n; // To store the sum of factorials // of the digits of n int sum = 0; while (n > 0) { // Get the last digit int d = n % 10; // Add the factorial of the current // digit to the sum sum += fact[d]; // Remove the last digit n /= 10; } if (sum == org) return true; return false; } // Driver code public static void Main (String[] args) { int n = 40585; if (isFactorion(n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Mohit kumar
Javascript
<script> // Javascript implementation of the approach const MAX = 10; // Function that returns true // if n is a Factorion function isFactorion(n) { // fact[i] will store i! let fact = new Array(MAX); fact[0] = 1; for (let i = 1; i < MAX; i++) fact[i] = i * fact[i - 1]; // A copy of the given integer let org = n; // To store the sum of factorials // of the digits of n let sum = 0; while (n > 0) { // Get the last digit let d = n % 10; // Add the factorial of the current // digit to the sum sum += fact[d]; // Remove the last digit n = parseInt(n / 10); } if (sum == org) return true; return false; } // Driver code let n = 40585; if (isFactorion(n)) document.write("Yes"); else document.write("No"); </script>
Yes
Complejidad de tiempo: O (log 10 n)
Espacio Auxiliar: O(MAX)
Publicación traducida automáticamente
Artículo escrito por AtomicRogue1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA