Trying to create a rutine to find the factorial of a number.

G

Guest

Ok, this is what i am tying to do:

I want to create a rutine that takes a value from a text box. Lets say for
example 5. and does the following 5*4*3*2*1 = 120


the rutine should be able to take any number and do the same:

if it is 9 then it should multiply: 9*8*7*6*5*4*3*2*1

if it is 3 then it should multiply: 3*2*1

I was working with this one but didnt get what i wanted:

Function Factorial(ByVal N As Integer) As Integer
If N <= 1 Then ' Reached end of recursive calls.
Return 1 ' N = 0 or 1, so climb back out of calls.
Else ' N > 1, so call Factorial again.
Return Factorial(N - 1) * N
End If
End Function


If someone has any suggestions,

Thanks,
 
G

Gregory Gadow

Eduardo78 said:
Ok, this is what i am tying to do:

I want to create a rutine that takes a value from a text box. Lets say for
example 5. and does the following 5*4*3*2*1 = 120

the rutine should be able to take any number and do the same:

if it is 9 then it should multiply: 9*8*7*6*5*4*3*2*1

if it is 3 then it should multiply: 3*2*1

I was working with this one but didnt get what i wanted:

Function Factorial(ByVal N As Integer) As Integer
If N <= 1 Then ' Reached end of recursive calls.
Return 1 ' N = 0 or 1, so climb back out of calls.
Else ' N > 1, so call Factorial again.
Return Factorial(N - 1) * N
End If
End Function

If someone has any suggestions,

That looks like the standard algorithm, and it is working correctly for me. Why
is it not working as you expect?
 
G

Guest

Eduardo78,

What about your code don't you like? It seems to give the right answer for
the values you used as examples.

Kerry Moorman
 
G

Guest

I was working with this one but didnt get what i wanted:
Function Factorial(ByVal N As Integer) As Integer
If N <= 1 Then ' Reached end of recursive calls.
Return 1 ' N = 0 or 1, so climb back out of calls.
Else ' N > 1, so call Factorial again.
Return Factorial(N - 1) * N
End If
End Function

Your Factorial function is correct, so I don't understand what you mean by
"didnt get what i wanted". You might want to change Integer to Long, because
you will overflow an integer with a fairly small value of N. (Somewhere near
10 for integer, 20 for long). Alternatively, use Double, and Double will
effectively retain precision because factorials are divisible by large-ish
powers of two.

If you need factorials of very large numbers, the way to go is to sacrifice
precision and use the LogGamma function which returns the natural logarithm
of the Gamma function.
 
H

Herfried K. Wagner [MVP]

Kerry Moorman said:
What about your code don't you like? It seems to give the right answer for
the values you used as examples.

That's true. However, I would prefer an iterative implementation using a
'For...To' loop over a recursive one.
 
G

Guest

Herfried,

Yes, except as an exercise in recursion, an iterative solution for
factorials (as for most things) is the way to go.

Kerry Moorman
 
C

Cor Ligthert [MVP]

Herfried,
That's true. However, I would prefer an iterative implementation using a
'For...To' loop over a recursive one.

Do they use in Austria as well that term "Bit f..king"

:)

Cor
 
H

Herfried K. Wagner [MVP]

Kerry Moorman said:
Yes, except as an exercise in recursion, an iterative solution for
factorials (as for most things) is the way to go.

The OP's post looks like an exercise ;-).
 
C

Cor Ligthert [MVP]

Herfried,

If it is discussing about to win a bit or a byte than we call that in Dutch
bitneuken, meaning in German bitbumsen, in English bit f..king.

I don't know it this is an International phrase.

:)

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
If it is discussing about to win a bit or a byte than we call that in
Dutch bitneuken, meaning in German bitbumsen, in English bit f..king.

Mhm... Imagine calculating ~ 100! This would require ~ 100 function calls
to be put on the stack.
 
G

Gregory Gadow

Herfried K. Wagner said:
That's true. However, I would prefer an iterative implementation using a
'For...To' loop over a recursive one.

Ah, you should have said something in your first post :) That's easy enough:

Public Function Factorial(ByVal X As Integer) As Integer
Dim i As Integer
Dim result As Integer

result = 1
For i = 2 To X
result *= i
Next
Return result
End Function

I agree that an iterative method would be more efficient for larger factorials.
For maximum efficiency, you might want to have the function return a UInteger,
the unsigned 32-bit data type. UIntegers are not CLS compliant, however; if that
is an issue, use Long.
 
J

Jay B. Harlow [MVP - Outlook]

Herfried,
Yes I thought the OPs question sounded an awful lot like homework!

As Factorial is a classic recursion problem (without using recursion) in
Computer Science classes (at least it was when I was in school, many many
moons ago)...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| > Yes, except as an exercise in recursion, an iterative solution for
| > factorials (as for most things) is the way to go.
|
| The OP's post looks like an exercise ;-).
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
 
H

Herfried K. Wagner [MVP]

Jay B. Harlow said:
Yes I thought the OPs question sounded an awful lot like homework!

As Factorial is a classic recursion problem (without using recursion) in
Computer Science classes (at least it was when I was in school, many many
moons ago)...

My sister (physics student) is currently learning C++ at university. One of
the first exercixe in the class was iterative calculation of n!. Maybe
they'll learn what recursion is too :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top