📜  阶乘及其邻居的LCM

📅  最后修改于: 2021-04-27 22:14:20             🧑  作者: Mango

给定一个数字,我们需要找到数字阶乘的LCM及其邻居。如果数字为N,则需要找到(N-1)!, N!的LCM。和(N + 1)!。这里N总是大于或等于1
例子 :

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.

方法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!,5!和6!的LCM。
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 
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


Javascript


输出:
720