给定一个数字,我们需要找到 阶乘的 数字和它的邻居。如果数字是N,我们需要找到(N-1)的LCM!,N和(N+1)!。 这里N总是大于或等于1 例如:
null
Input : N = 5 Output : 720ExplanationHere the given number is 5, its neighbors are 4 and 6. The factorial of these three numbers are 24, 120, and 720.so the LCMof 24, 120, 720 is 720. Input : N = 3 Output : 24ExplanationHere the given number is 3, its Neighborsare 2 and 4.the factorial of these three numbers are 2, 6, and 24. So the LCM of 2, 6 and 24 is 24.
方法1(简单) 我们首先计算数的阶乘,然后计算它的邻居的阶乘 找出这些阶乘数的LCM。 方法2(高效) 我们可以看到(N-1)!,N和(N+1)!总是(N-1)!*N!*(N+1)! 这可以写成(N-1)!*N*(N-1)!*(N+1)*N*(N-1)! 所以LCM变成(N-1)!*N*(N+1) 这就是(N+1)! 实例 N=5 我们需要找到4的LCM!,5.还有6个! LCM共4个!,5.还有6个! = 4! * 5! * 6. = 4! * 5*4! * 6*5*4! = 6*5*4! = 720 所以我们可以说,三个连续数的阶乘的LCM总是最大数的阶乘。在这种情况下(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) << "" ; 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END