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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program for Stirling Interpolation Formula
- Inbuilt function for calculating LCM in C++
- Bakhshali Approximation for computing square roots
- Transportation Problem | Set 4 (Vogel's Approximation Method)
- 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
- Product of first N factorials
- GCD of factorials of two numbers
- Queries for the 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
- Trailing number of 0s in product of two factorials
- Minimum number of Factorials whose sum is equal to N
- Sum of factorials of Prime numbers in a Linked list
- Count natural numbers whose factorials are divisible by x but not y
- Print factorials of a range in right aligned format
- Find the unit place digit of sum of N factorials
- Check if a given number divides the sum of the factorials of its digits