Procedure.q Factoral(n.l)
If n >= 0
If n > 0
Define answer.q = 1
While n > 0
answer = answer * n
n - 1
Wend
ProcedureReturn answer
Else
ProcedureReturn 1
EndIf
Else
ProcedureReturn -1
EndIf
EndProcedure
When I use a number greater than 20 I get an incorrect result (granted it is a very large number). How can I correct this so that it gives me correct results? Is there a way to deal with very large numbers?
Thanks.
-- DB
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.
If i remember correctly Rings once did a big number lib, which uses strings to hold the numbers. This ofcourse comes with a penalty in speed as the calculations more or less needs to mimic the way it's done manually with pen and paper..
Procedure Factoral(n.l)
M = 2 * n
Dim P(2 * n)
P(M) = 1
For I = 1 To M/2
For F = M To 1 Step -1
P(F) = P(F) * I
Next F
For F = M To 1 Step -1
P(F-1) = P(F-1) + Int(P(F)/10)
P(F) = P(F)-10 * Int(P(F)/10)
Next F
For F = 1 To M
If P(F) = 0
S = F
EndIf
If P(F) > 0
F = M
EndIf
Next F
For F = S + 1 To M
Fak$ = Fak$ + Str(P(F))
Next F
Debug Str(I) + "!" + " = " + Fak$
Fak$ = ""
Next I
EndProcedure
Factoral(100)
Global Fak1$
Procedure Factoral(n)
M = 3 * n
Dim P(3 * n)
P(M) = 1
For I = 1 To M/2
For F = M To 1 Step -1
P(F) = P(F) * I
Next F
For F = M To 1 Step -1
P(F-1) = P(F-1) + Int(P(F)/10)
P(F) = P(F)-10 * Int(P(F)/10)
Next F
For F = 1 To M
If P(F) = 0
S = F
EndIf
If P(F) > 0
F = M
EndIf
Next F
For F = S + 1 To M
Fak$ = Fak$ + Str(P(F))
Next F
If I = n
Fak1$=Fak$
Break
EndIf
Fak$ = ""
Next I
EndProcedure
Factoral(1000)
Debug Fak1$
This is great but what I'd like to know is, what is the largest number I could deal with in my original function. Is there a way get it do deal with larger (real) numbers and not output a string.
ps. Why did you declare a global variable and then reference it in the procedure. That's not a great practice. It is better if all the procedure variables are local and you use procedurereturn to pass the output back.
Thanks for the help though.
-- DB
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.
@PB&J Lover
the biggest number of a signed quad is
(2^63)-1 = 9.223.372.036.854.775.807
Therefore your code works up to
20! = 2.432.902.008.176.640.000, because
21! = 51.090.942.171.709.440.000 > (2^63)-1