We are aware of calculating factorials using loops or recursion, but if we are asked to calculate factorial without using any loop or recursion. Yes, this is possible through a well-known approximation algorithm known as Stirling approximation.
Input : n = 6 Output : 720 Input : n = 2 Output : 2
Stirling approximation: is an approximation for calculating factorials. It is also useful for approximating the log of a factorial.
n! ~ sqrt(2*pi*n) * pow((n/e), n)
Note: This formula will not give the exact value of the factorial because it is just the approximation of the factorial.
1 1 5 23 119 723 5086
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Program for Stirling Interpolation Formula
- Inbuilt function for calculating LCM in C++
- Bakhshali Approximation for computing square roots
- Calculating n-th real root using binary search
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- GCD of factorials of two numbers
- Product of first N factorials
- Find last two digits of sum of N factorials
- Find sum of factorials in an array
- Numbers whose factorials end with n zeros
- Queries for the product of first N factorials
- Trailing number of 0s in product of two factorials
- Count natural numbers whose factorials are divisible by x but not y
- Check if a given number divides the sum of the factorials of its digits
- Print factorials of a range in right aligned format