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.
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.
- Program for Stirling Interpolation Formula
- Transportation Problem | Set 4 (Vogel's Approximation Method)
- Bakhshali Approximation for computing square roots
- Inbuilt function for calculating LCM in C++
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Calculating n-th real root using binary search
- GCD of factorials of two numbers
- Product of first N factorials
- Find sum of factorials in an array
- Numbers whose factorials end with n zeros
- Find last two digits of sum of N factorials
- Queries for the product of first N factorials
- Trailing number of 0s in product of two factorials
- Minimum number of Factorials whose sum is equal to N
- Find the unit place digit of sum of N factorials
- Sum of factorials of Prime numbers in a Linked list
- 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