# Calculating Factorials using Stirling Approximation

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// CPP program for calculating factorial  ` `// of a number using Stirling  ` `// Approximation  ` `#include  ` `using` `namespace` `std; ` ` `  `// function for calculating factorial ` `long` `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 program ` `int` `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

 `// Java program for calculating ` `// factorial of a number using  ` `// Stirling Approximation  ` `class` `GFG ` `{ ` `     `  `// function for  ` `// calculating factorial ` `public` `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 Code ` `public` `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

 `# Python3 program for calculating  ` `# factorial of a number using  ` `# Stirling Approximation  ` `import` `math ` ` `  `# Function for calculating factorial ` `def` `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 Code ` `print``(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#

 `// C# program for calculating ` `// factorial of a number using  ` `// Stirling Approximation  ` ` `  `class` `GFG ` `{ ` `     `  `// function for  ` `// calculating factorial ` `public` `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 Code ` `public` `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

 ` `

Output:

```1
1
5
23
119
723
5086
```

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.

My Personal Notes arrow_drop_up

Improved By : vt_m, Mithun Kumar, BournThing

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.