Brighton Webs Ltd.
Statistics for Energy and the Environment
Home Index About

Factorial

The factorial of an integer is defined as:

Factorial

The factorial of a number is shown as n!.

For calculating the factorial of large numbers, it can be advantageous to use the gamma function, for integer values of n, the relationship between the factorial and gamma function is:

Factorial and Gamma Function

The factorial of zero is one.

Example

The factorial of six is calculated:

Factorial Example

Pseudo Code

The VB style pseudo code below uses the multiplicative method to calculate the factorial of an integer in the range 0 to 128.  Repetitive multiplication will ultimately lead to overflow, hence the limitation.  The appropriate value of N_Max, will depend on the language and the precision used.

Private Function Factorial(N As Long) As Double

'Description

'Returns factorial of N, if N is less than N_Max

'otherwise 0 to indicate failure.

'Argument

'N an integer >= 0

'Variables

'N_Max an integer which if N exceeds will

'result in an overflow error.

'Declaration

Dim I As Long 'Loop Counter

Dim F As Double 'Factorial

Dim N_Max As Long

'N_Max to Preve nt Overflow

N_Max = 128

'Validate

If N < 0 Then

Exit Function

ElseIf N > N_Max Then

Exit Function

End If

'Initialise

F = 1

'Calculate

For I = 2 To N

F = I * F

Next I

'Return

Factorial = F

End Function

Page updated: 02-Feb-2006