Posts: 3,616
Threads: 287
Joined: Jan 2003
Code:
Function factorial(n As Double) As Double
If n < 2 Then
factorial = 1
Else
factorial = n * factorial(n - 1)
End if
End Function
That's my recursive factorial-figurer. But when I try to use it to calculate even the smallest factorials, like 3! or 4!, I get the runtime error "Out of stack space".
Is it just my computer?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Two things:
1.- Maybe you need to set up an auxiliary variable to store the results, and then assign it to factorial (I'm not sure, but that's how I do it). Maybe that "factorial = 1" in the base case is doing nasty things (senseless calls and stuff).
2.- Upon the definition of factorial it only works with integer numbers, so it would be less correct to use LONGs (you can use Doubles if you are careful enough not to send decimal numbers to the function).
Code:
Function factorial(n As Long) As Long
Dim res As Long
If n < 2 Then
res = 1
Else
res = n * factorial(n - 1)
End if
factorial = res
End Function
Posts: 1,845
Threads: 44
Joined: Aug 2002
VB doesn't have a very large stack... :-?
Anyway, why do you need a recursive function to calculate the factorials?
Can't you just do this:
Code:
Private Function Fac (n As Integer) As Long
If n <= 1 Then Fac = 1: Exit Function
Dim lngRes As Long, intT As Integer
lngRes = 1
For intT = 2 To n
lngRes = lngRes * intT
Next intT
Fac = lngRes
End Function
Works faster and cleaner and doesn't need as much stack space as the recursive function
Posts: 3,616
Threads: 287
Joined: Jan 2003
Neo: True, true, but I wanted for practice in computer sci to write this thing.
In QB, isn't there a command to allocate more stack space? Does that apply to QB as well?
[EDIT]Nath, thanks, doing that worked. [/EDIT]
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 4,307
Threads: 63
Joined: May 2003
I dunno whats wrong with our CP teacher as well. They insist on using recursion as well :???:
Posts: 1,752
Threads: 21
Joined: Jun 2002
Recursion is very useful at times...for instance, walking a directory structure or writing a threaded forum.
Posts: 4,307
Threads: 63
Joined: May 2003
In those cases Yes. But not in case of factorials and such stuff :wink:
Posts: 3,616
Threads: 287
Joined: Jan 2003
Well, hey...it was in the vb textbook I'm studying, and I've encountered it before (recursion) and I never really understood it. So I thought, why not implement it in a *Very* easy language?
(I've done it in assembly before, a nightmare).
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Quote:In those cases Yes. But not in case of factorials and such stuff :wink:
Well, that's only for educational purposes. Factorial is itself a really simple function to illustrate the base case and the recursive case, it is short, and can be understood easily.