Calculating Factorials using Stirling Approximation
Last Updated :
15 Sep, 2023
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++
#include <bits/stdc++.h>
using namespace std;
long int stirlingFactorial( int n)
{
if (n == 1)
return 1;
long int z;
float e = 2.71;
z = sqrt (2 * 3.14 * n) * pow ((n / e), n);
return z;
}
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
class GFG
{
public static int stirlingFactorial( double n)
{
if (n == 1 )
return 1 ;
double z;
double e = 2.71 ;
z = Math.sqrt( 2 * 3.14 * n) *
Math.pow((n / e), n);
return ( int )(z);
}
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 ));
}
}
|
Python3
import math
def stirlingFactorial(n):
if (n = = 1 ):
return 1
e = 2.71
z = (math.sqrt( 2 * 3.14 * n) * math. pow ((n / e), n))
return math.floor(z)
print (stirlingFactorial( 1 ))
print (stirlingFactorial( 2 ))
print (stirlingFactorial( 3 ))
print (stirlingFactorial( 4 ))
print (stirlingFactorial( 5 ))
print (stirlingFactorial( 6 ))
print (stirlingFactorial( 7 ))
|
C#
class GFG
{
public static int stirlingFactorial( double n)
{
if (n == 1)
return 1;
double z;
double e = 2.71;
z = System.Math.Sqrt(2 * 3.14 * n) *
System.Math.Pow((n / e), n);
return ( int )(z);
}
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));
}
}
|
PHP
<?php
function stirlingFactorial( $n )
{
if ( $n == 1)
return 1;
$z ;
$e = 2.71;
$z = sqrt(2 * 3.14 * $n ) *
pow(( $n / $e ), $n );
return floor ( $z );
}
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" ;
?>
|
Javascript
<script>
function stirlingFactorial(n)
{
if (n == 1)
return 1;
let z;
let e = 2.71;
z = Math.sqrt(2 * 3.14 * n) *
Math.pow((n / e), n);
return Math.floor(z);
}
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>" );
</script>
|
Time complexity: O(logn)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...