Domino Code Fragment
Code Name* Factorial Function | Date* 04/28/2024 | Source (or email address if you prefer)* Rlatulippe@romac.com IP address:.3.141.152.173 | |
Description* Receives nFact as the user input plus additional parameters for displaying each factorial between zero and nFact as well as the number of recursive calls. The function returns the factorial of nFact . | Type* LotusScript | Categories* (Misc) |
Implementation: | Required Client: | Server: |
Limitations: | Comments: |
Function Factorial (nFact As Double, sTrack As String, sRecalled As String, nRecall As Integer) As Double.
Receives nFact as the user input plus additional parameters for displaying each factorial between zero and nFact as well as the number of recursive calls. The function returns the factorial of nFact.
Function Syntax
Function Factorial( nFact , sTrack , sRecalled, nRecall )
Formal Parameters
nFact
Number input by user.
sTrack
Track factorial equations for display using messagebox.
sRecalled
Build string of recursive calls,
nRecall
nRecall number of recursive calls.
Return value
Factorial of nFact.
Function
'-- nFact = Number user input, sTrack = track factorial equations for display, sRecalled = build string of recursive calls, nRecall number of recursive call being worked on.
Function Factorial (nFact As Double, sTrack As String, sRecalled As String, nRecall As Integer) As Double
Dim nTotal As Integer
nTotal = 1
Dim ncount As Integer
If nFact = 0 Then
strack = strack & Chr(13) & Cstr(0) & "! = " & Cstr(1) & Chr(13) & Chr(13)
'-- Format Recursive calls for display to screen
sRecalled = sRecalled & Chr(13) & Cstr(nRecall) & Chr(13) & Chr(13)
Factorial = 1 '-- End of Recursive call...This is the base case
Else
'-- loop to gain current factorial answer for adding to sTrack string
ncount = nFact
For i = 1 To nFact
If ncount > 1 Then
nTotal = ncount * nTotal
ncount = ncount - 1
End If
Next i
'-- format factorial information for display to screen
strack = strack & Chr(13) & Cstr(nFact) & "! = " & Cstr(nFact) &"*" & Cstr(nFact - 1) & "! = " & Cstr(ntotal) &_
Chr(13) & Chr(13)
'-- Format Recursive calls for display to screen
sRecalled = sRecalled & Chr(13) & Cstr(nRecall) & Chr(13) & Chr(13)
nRecall = nRecall + 1
Factorial =nFact * Factorial (nFact - 1, sTrack, sRecalled, nRecall) '-- This is the recursive case is
End If
End Function
Usage
The valid values for the nFact is an positive value.
Example
This example ask the user for a positive number as input and returns that numbers factorial. Uses above parameters to display all the factorial numbers between 0 and nFact as well as depth of the recusive calls in Messagebox.
Sub Click(Source As Button)
'-- Number input by user
Dim nNumber As Double
'-- Temp storage for nNumber
Dim nTemp As Double
'-- Integer to track number of recursive calls
Dim nRecall As Integer
'-- String to track factorial data
Dim sTrack As String
'-- String to record recursive calls and match them to factorial being computed at time
Dim sRecalled As String
nRecall = 1
'-- Get number from user
nNumber = Cdbl(Inputbox("Enter a positive number", "Factorial"))
'-- assign input to temp variable as number will change as recursively called
nTemp = nNumber
'-- Call the function Factorial
nNumber = Factorial(nNumber, sTrack, sRecalled ,nRecall)
Messagebox "Factorial of " & Cstr( nTemp ) & " is" & Chr(13) &_ sTrack,, "Recusive calls = " & Cstr(nRecall)
End Sub