Calculating Factorials using Stirling Approximation - GeeksforGeeks

07 Apr, 2021
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. Examples:  
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. 
C++
Java
Python3
C#
PHP
Javascript
// CPP program for calculating factorial// of a number using Stirling// Approximation#include<bits/stdc++.h>using namespace std; // function for calculating factoriallong int stirlingFactorial(int n){    if (n == 1)        return 1;    long int z;    float e = 2.71; // value of natural e         // evaluating factorial using    // stirling approximation    z = sqrt(2*3.14*n) * pow((n/e), n);    return z;} // driver programint main(){    cout << stirlingFactorial(1) << endl;    cout << stirlingFactorial(2) << endl;    cout << stirlingFactorial(3) << endl;    cout << stirlingFactorial(4) << endl;    cout << stirlingFactorial(5) << endl;    cout << stirlingFactorial(6) << endl;    cout << stirlingFactorial(7) << endl;    return 0;}
// Java program for calculating// factorial of a number using// Stirling Approximationclass GFG{     // function for// calculating factorialpublic static int stirlingFactorial(double n){    if (n == 1)        return 1;    double z;    double e = 2.71; // value of natural e         // evaluating factorial using    // stirling approximation    z = Math.sqrt(2 * 3.14 * n) *        Math.pow((n / e), n);    return (int)(z);} // Driver Codepublic static void main(String[] args){    System.out.println(stirlingFactorial(1));    System.out.println(stirlingFactorial(2));    System.out.println(stirlingFactorial(3));    System.out.println(stirlingFactorial(4));    System.out.println(stirlingFactorial(5));    System.out.println(stirlingFactorial(6));    System.out.println(stirlingFactorial(7));}} // This code is contributed by mits.
# Python3 program for calculating# factorial of a number using# Stirling Approximationimport math # Function for calculating factorialdef stirlingFactorial(n):    if (n == 1):        return 1         # value of natural e    e = 2.71         # evaluating factorial using    # stirling approximation    z = (math.sqrt(2 * 3.14 * n) * math.pow((n / e), n))    return math.floor(z) # Driver Codeprint(stirlingFactorial(1))print(stirlingFactorial(2))print(stirlingFactorial(3))print(stirlingFactorial(4))print(stirlingFactorial(5))print(stirlingFactorial(6))print(stirlingFactorial(7)) # This code is contributed by mits
// C# program for calculating// factorial of a number using// Stirling Approximation class GFG{     // function for// calculating factorialpublic static int stirlingFactorial(double n){    if (n == 1)        return 1;    double z;    double e = 2.71; // value of natural e         // evaluating factorial using    // stirling approximation    z = System.Math.Sqrt(2 * 3.14 * n) *        System.Math.Pow((n / e), n);    return (int)(z);} // Driver Codepublic static void Main(){    System.Console.WriteLine(stirlingFactorial(1));    System.Console.WriteLine(stirlingFactorial(2));    System.Console.WriteLine(stirlingFactorial(3));    System.Console.WriteLine(stirlingFactorial(4));    System.Console.WriteLine(stirlingFactorial(5));    System.Console.WriteLine(stirlingFactorial(6));    System.Console.WriteLine(stirlingFactorial(7));}} // This code is contributed by mits.
<?php// PHP program for calculating factorial// of a number using Stirling// Approximation // Function for calculating factorialfunction stirlingFactorial($n){    if ($n == 1)        return 1;    $z;         // value of natural e    $e = 2.71;         // evaluating factorial using    // stirling approximation    $z = sqrt(2 * 3.14 * $n) *         pow(($n / $e), $n);    return floor($z);}     // Driver Code    echo stirlingFactorial(1),"\n";    echo stirlingFactorial(2) ,"\n";    echo stirlingFactorial(3) ,"\n";    echo stirlingFactorial(4), "\n" ;    echo stirlingFactorial(5) ,"\n";    echo stirlingFactorial(6) ," \n";    echo stirlingFactorial(7) ," \n"; // This code is contributed by anuj_67.?>
<script>// Javascript program for calculating factorial// of a number using Stirling// Approximation // Function for calculating factorialfunction stirlingFactorial(n){    if (n == 1)        return 1;    let z;         // value of natural e    let e = 2.71;         // evaluating factorial using    // stirling approximation    z = Math.sqrt(2 * 3.14 * n) *         Math.pow((n / e), n);    return Math.floor(z);}     // Driver Code    document.write( stirlingFactorial(1) + "<br>");    document.write( stirlingFactorial(2) + "<br>");    document.write( stirlingFactorial(3) + "<br>");    document.write( stirlingFactorial(4) + "<br>");    document.write( stirlingFactorial(5) + "<br>");    document.write( stirlingFactorial(6) + "<br>");    document.write( stirlingFactorial(7) + "<br>"); // This code is contributed by _saurabh_jaiswal.</script>
Output:  
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 contribute@geeksforgeeks.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. 
vt_m
Mithun Kumar
BournThing
_saurabh_jaiswal
factorial
Mathematical
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Check if a number is Palindrome
Count ways to reach the n'th stair
Find first and last digits of a number
Find Union and Intersection of two unsorted arrays
Program to convert a given number to words