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.
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.
- Count natural numbers whose factorials are divisible by x but not y
- Numbers whose factorials end with n zeros
- Trailing number of 0s in product of two factorials
- Queries for the product of first N factorials
- GCD of factorials of two numbers
- Print factorials of a range in right aligned format
- Product of first N factorials
- Find the unit place digit of sum of N factorials
- Find last two digits of sum of N factorials
- Check if a given number divides the sum of the factorials of its digits
- Find sum of factorials in an array
- Minimum number of Factorials whose sum is equal to N
- Sum of factorials of Prime numbers in a Linked list
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Count different numbers possible using all the digits their frequency times
- Find the number of strings formed using distinct characters of a given string
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Factorial of Large numbers using Logarithmic identity