Given a number, we need to find LCM of the factorial of the numbers and its neighbors. If the number is N, we need to find LCM of (N-1)!, N! and (N+1)!.**Here N is always greater than or equal too 1**

**Examples :**

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 LCM of 24, 120, 720 is 720. Input : N = 3 Output : 24ExplanationHere 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.

**Method 1(Simple)**. We first calculate the factorial of number and and the factorial of its neighbor then

find the LCM of these factorials numbers.

**Method 2(Efficient)**

We can see that the LCM of (N-1)!, N! and (N+1)! is always (N-1)! * N! * (N+1)!

this can be written as (N-1)! * N*(N-1)! * (N+1)*N*(N-1)!

so the LCM become (N-1)! * N * (N+1)

which is (N+1)!

**Example**

N = 5

We need to find the LCM of 4!, 5!and 6!

LCM of 4!, 5!and 6!

= 4! * 5! * 6!

= 4! * 5*4! * 6*5*4!

= 6*5*4!

= 720

So we can say that LCM of the factorial of three consecutive numbers is always the factorial of the largest number.in this case (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; ` `} ` |

*chevron_right*

*filter_none*

## 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.*/` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

720

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Find the last digit when factorial of A divides factorial of B
- Fill array with 1's using minimum iterations of filling neighbors
- Puzzle | Neighbors in a round table
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Smallest N digit number with none of its digits as its divisor
- GCD, LCM and Distributive Property
- Given GCD G and LCM L, find number of possible pairs (a, b)
- Levelwise Alternating GCD and LCM of nodes in Segment Tree
- Find any pair with given GCD and LCM
- Find the other number when LCM and HCF given
- LCM and HCF of fractions
- Minimum LCM and GCD possible among all possible sub-arrays
- Count the number of subsequences of length k having equal LCM and HCF
- Find two numbers with the given LCM and minimum possible difference
- Number of pairs such that their HCF and LCM is equal
- Print any pair of integers with sum of GCD and LCM equals to N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.