factorial Infinity Overflow.....

yjsang92

New member
Joined
Jan 31, 2013
Messages
1
Programming Experience
Beginner
This is what I have for a factorial equation but everytime I input a number over 34 it gives me infinity....
Is there a way where I can input numbers greater than 34 and it still shows me the results of it?

Thanks



Dim ninput
As Long, routput As Long
Dim result As Long, a As Long


If Not IsNumeric(n_val.Text) Then

MsgBox(
"Please Input a Number")


Exit Sub


End If


If n_val.Text < 0 Then

MsgBox(
"Please Input a Positive Number")


Exit Sub


End If

ninput = Val(n_val.Text)


If Not Val(n_val.Text) = Int(ninput) Then

MsgBox(
"Please Input a Whole Number")


Exit Sub


End If


If ninput <= 1 Then

routput = 1


End If

result = 1


For a = 2 To ninput Step 1

result = result * a


Next a

r_val.Text = Str(result)
 
That factorial equation is actually exponential and goes to infinity... I am pretty sure you are not seeing how big the numbers are coming out, you overflow 64 bits after the 20th multiplication. If you do intend to factor numbers this big use a BigInteger as suggested, but keep in mind the more you advance the faster it heads towards infinity... My guess is you will fill your RAM if you enter a value like 1000.
 
Back
Top