Calculating Factorial in DS Routine returning junk values
Moderators: chulett, rschirm, roy
Calculating Factorial in DS Routine returning junk values
Hi
I am calculating Factorial in a DS routine. The function is returning correct values except when I pass large integers to the function eg Factorial(800).
It is returning junk values like '#1 IND'.
Can anybody help in this regard.
Or any other way to calculate factorial.
I am attaching the function below that I am using :
============================================
Factorial(n) :
Ans = 1
If n=0 Then Ans=1
If n>0 Then
intStartPos = n
intEndPos = 1
Loop
If intStartPos = intEndPos Then Exit
Ans = Ans * intStartPos
intStartPos = intStartPos - 1
Repeat
End
=============================================
I am calculating Factorial in a DS routine. The function is returning correct values except when I pass large integers to the function eg Factorial(800).
It is returning junk values like '#1 IND'.
Can anybody help in this regard.
Or any other way to calculate factorial.
I am attaching the function below that I am using :
============================================
Factorial(n) :
Ans = 1
If n=0 Then Ans=1
If n>0 Then
intStartPos = n
intEndPos = 1
Loop
If intStartPos = intEndPos Then Exit
Ans = Ans * intStartPos
intStartPos = intStartPos - 1
Repeat
End
=============================================
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Why are you replicating rumu's work?
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
- Hypgeomdist in Datastage
Hi
Myself and Rumu are in the same team.
Actually we are trying to implement the HYPGEOMDIST() function that you find in Ms Excel worksheet and we failing when large values are passed to it. Here goes the code . Can anybody help :
==============================================
HypgeomDist :
DEFFUN Combination(p,q) Calling "DSU.Combination"
Ans =0
Flag=1
If (x <0 ) or ( n < 0 ) or ( M<0 ) or ( Z <0 ) Then Flag = -1
If (x > M ) or ( n > Z) Then Flag = 0
If Flag > 0 Then
Val_1= Combination ( M,x)
Val_2= Combination ( Z-M ,n-x )
Val_3 = Combination ( Z ,n )
Ans = (Val_1 * Val_2)/ (Val_3)
End
Else Ans = Flag
===============================================
Combination :
DEFFUN Factorial(n) Calling "DSU.Factorial"
Val_1=Factorial (p)
Val_2= Factorial (q)
Val_3= Factorial (p-q)
Ans= Val_1/Val_2*Val_3
============================================
Factorial :
Ans = 1
If n=0 Then Ans=1
If n>0 Then
intStartPos = n
intEndPos = 1
Loop
If intStartPos = intEndPos Then Exit
Ans = Ans * intStartPos
intStartPos = intStartPos - 1
Repeat
End
Myself and Rumu are in the same team.
Actually we are trying to implement the HYPGEOMDIST() function that you find in Ms Excel worksheet and we failing when large values are passed to it. Here goes the code . Can anybody help :
==============================================
HypgeomDist :
DEFFUN Combination(p,q) Calling "DSU.Combination"
Ans =0
Flag=1
If (x <0 ) or ( n < 0 ) or ( M<0 ) or ( Z <0 ) Then Flag = -1
If (x > M ) or ( n > Z) Then Flag = 0
If Flag > 0 Then
Val_1= Combination ( M,x)
Val_2= Combination ( Z-M ,n-x )
Val_3 = Combination ( Z ,n )
Ans = (Val_1 * Val_2)/ (Val_3)
End
Else Ans = Flag
===============================================
Combination :
DEFFUN Factorial(n) Calling "DSU.Factorial"
Val_1=Factorial (p)
Val_2= Factorial (q)
Val_3= Factorial (p-q)
Ans= Val_1/Val_2*Val_3
============================================
Factorial :
Ans = 1
If n=0 Then Ans=1
If n>0 Then
intStartPos = n
intEndPos = 1
Loop
If intStartPos = intEndPos Then Exit
Ans = Ans * intStartPos
intStartPos = intStartPos - 1
Repeat
End
For those interested in recursion, you could write a very short factorial program for string math as follows:
Factorial(Arg1)
Factorial(Arg1)
Code: Select all
DEFFUN Factorial(Arg1) CALLING "DSU.Factorial"
IF Arg1="2" THEN Ans = "2" ELSE Ans = SMUL(Arg1,Factorial(SSUB(Arg1,1)))
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Factorial routine
The following routine returns factorial of large nos. when input svFactorial is supplied to be calculated
Ans = 1.0
n=svFactorial
If n=0 Then Ans=1
If n>1 Then
LOOP
while n>1 DO
Ans = smul(Ans,n)
n=n-1
continue
REPEAT
End
Ans = 1.0
n=svFactorial
If n=0 Then Ans=1
If n>1 Then
LOOP
while n>1 DO
Ans = smul(Ans,n)
n=n-1
continue
REPEAT
End
Ankush
It seems silly to recompute fixed numbers. Would you write the sieve of eratosthenes algorithm in a function to test a value as a prime number or would you rather use a lookup to a predefined list of prime values?
It's an important question because there's a definition of an insanity: doing the same thing over and over expecting different results each time. No matter how many times you test 42 as a prime number the answer will always be NO.
No matter how many times you compute 42! the answer will be 1405006117752879898543142606244511569936384000000000.
The sieve is an algorithm used to tax and measure the cpu computing ability of processors, why the heck would you ever use it in a function? Likewise, a factorial algorithm is in the same nature. If your process calls your factorial function 10000000 times to compute 42!, aren't you wasting a lot of cpu cycles for nothing? There's never going to be a different answer!
You would be better off either using a hashed lookup in a GUI based design, or reading a text file of results into a COMMON array once and doing an array lookup.
It's an important question because there's a definition of an insanity: doing the same thing over and over expecting different results each time. No matter how many times you test 42 as a prime number the answer will always be NO.
No matter how many times you compute 42! the answer will be 1405006117752879898543142606244511569936384000000000.
The sieve is an algorithm used to tax and measure the cpu computing ability of processors, why the heck would you ever use it in a function? Likewise, a factorial algorithm is in the same nature. If your process calls your factorial function 10000000 times to compute 42!, aren't you wasting a lot of cpu cycles for nothing? There's never going to be a different answer!
You would be better off either using a hashed lookup in a GUI based design, or reading a text file of results into a COMMON array once and doing an array lookup.
Kenneth Bland
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle