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 email@example.com. 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
- Product of first N factorials
- GCD of factorials of two numbers
- Find last two digits of sum of N factorials
- Numbers whose factorials end with n zeros
- Trailing number of 0s in product of two factorials
- Find the unit place digit of sum of N 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