Dado un número, necesitamos encontrar el MCM del factorial de los números y sus vecinos. Si el número es N, ¡necesitamos encontrar MCM de (N-1)!, N! y (N+1)!. Aquí N siempre es mayor o igual que 1
Ejemplos:
Input : N = 5 Output : 720 Explanation Here the given number is 5, its neighbors are 4 and 6. The factorial of these three numbers are 24, 120, and 720.so the LCM of 24, 120, 720 is 720. Input : N = 3 Output : 24 Explanation Here the given number is 3, its Neighbors are 2 and 4.the factorial of these three numbers are 2, 6, and 24. So the LCM of 2, 6 and 24 is 24.
Método 1 (Simple) . Primero calculamos el factorial del número y el factorial de su vecino y luego
encontramos el MCM de estos números factoriales.
Método 2 (Eficiente)
Podemos ver que el MCM de (N-1)!, N! y (N+1)! es siempre (N-1)! * N! * (N+1)!
esto se puede escribir como (N-1)! * N*(N-1)! * (N+1)*N*(N-1)!
¡entonces el LCM se convierte en (N-1)! * N * (N+1)
que es (N+1)!
Ejemplo
N = 5
¡Necesitamos encontrar el MCM de 4!, 5! y 6!
MCM de 4!, 5! y 6!
= 4! * 5! * 6!
= 4! * 5*4! * 6*5*4!
= 6*5*4!
= 720
Entonces podemos decir que el MCM del factorial de tres números consecutivos es siempre el factorial del número mayor. En este caso (N+1)!.
C++
// CPP program to calculate the LCM of N! // and its neighbor (N-1)! and (N+1)! #include <bits/stdc++.h> using namespace std; // function to calculate the factorial unsigned int factorial(unsigned int n) { if (n == 0) return 1; return n * factorial(n - 1); } int LCMOfNeighbourFact(int n) { // returning the factorial of the // largest number in the given three // consecutive numbers return factorial(n + 1); } // Driver code int main() { int N = 5; cout << LCMOfNeighbourFact(N) << "\n"; return 0; }
Java
// Java program to calculate the LCM of N! // and its neighbor (N-1)! and (N+1)! import java.io.*; class GFG { // function to calculate the factorial static int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } static int LCMOfNeighbourFact(int n) { // returning the factorial of the // largest number in the given three // consecutive numbers return factorial(n + 1); } // Driver code public static void main(String args[]) { int N = 5; System.out.println(LCMOfNeighbourFact(N)); } } /*This code is contributed by Nikita Tiwari.*/
Python3
# Python3 program to calculate the LCM of N! # and its neighbor (N-1)! and (N+1)! # Function to calculate the factorial def factorial(n): if (n == 0): return 1 return n * factorial(n - 1) def LCMOfNeighbourFact(n): # returning the factorial of the # largest number in the given three # consecutive numbers return factorial(n + 1) # Driver code N = 5 print(LCMOfNeighbourFact(N)) # This code is contributed by Anant Agarwal.
C#
// Program to calculate the LCM // of N! and its neighbor (N-1)! // and (N+1)! using System; class GFG { // function to calculate the factorial static int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } static int LCMOfNeighbourFact(int n) { // returning the factorial of the // largest number in the given three // consecutive numbers return factorial(n + 1); } // Driver code public static void Main() { int N = 5; Console.WriteLine(LCMOfNeighbourFact(N)); } } // This code is contributed by Anant Agarwal.
PHP
<?php // PHP program to calculate // the LCM of N! and its neighbor // (N-1)! and (N+1)! // function to calculate // the factorial function factorial($n) { if ($n == 0) return 1; return $n * factorial($n - 1); } function LCMOfNeighbourFact($n) { // returning the factorial // of the largest number in // the given three // consecutive numbers return factorial($n + 1); } // Driver code $N = 5; echo(LCMOfNeighbourFact($N)); // This code is contributed by Ajit. ?>
Javascript
<script> // javascript program to calculate the LCM of N! // and its neighbor (N-1)! and (N+1)! // function to calculate the factorial function factorial(n) { if (n == 0) return 1; return n * factorial(n - 1); } function LCMOfNeighbourFact(n) { // returning the factorial of the // largest number in the given three // consecutive numbers return factorial(n + 1); } // Driver code var N = 5; document.write(LCMOfNeighbourFact(N)); // This code is contributed by aashish1995 </script>
720
Complejidad temporal: O(n)
Espacio auxiliar: O(n)
Sugiera si alguien tiene una mejor solución que sea más eficiente en términos de espacio y tiempo.
Este artículo es una contribución de Aarti_Rathi . Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por Shahnawaz_Ali y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA