Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleApproximate the factorial function with Sterling's formula
DescriptionThis example shows how to approximate the factorial function with Sterling's formula in Visual Basic 6.
KeywordsSterling's formula, factorial, calculation
CategoriesAlgorithms
 
Sterling's formula approximates the factorial function with:

    N! = Sqr(2 * PI * N) * (N / E) ^ N

This is a fairly good approximation for larger N. For example, when N = 10, the difference between this value and the true factorial is about 0.83 percent.

When you change the value in this program's text box, the code calculates the true factorial and the value given by Sterling's and displays the results and their difference.

 
Private Function Sterling(ByVal N As Double) As Double
Const PI = 3.141592653
Const E = 2.718281828

    Sterling = Sqr(2 * PI * N) * (N / E) ^ N
End Function
 
Note that Sterling's formula isn't all that useful for small N because is is less exact and even takes longer to calculate. When N gets big, however, calculating it directly will result in rounding errors so Sterling's formula may be more precise.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated