Page 1 of 1

Calculating Factorial in DS Routine returning junk values

Posted: Fri Jul 06, 2007 9:19 am
by kasgang
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

=============================================

Posted: Fri Jul 06, 2007 9:46 am
by ray.wurlod
Why are you replicating rumu's work?

- Hypgeomdist in Datastage

Posted: Sun Jul 08, 2007 7:16 am
by kasgang
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

Posted: Sun Jul 08, 2007 4:20 pm
by ArndW
Kasgang, the suggestion was already made to use string maths in DataStage if the number exceeds the limits imposed. It will significantly reduce the speed, but will be able to handle your factorial.

Posted: Sun Jul 08, 2007 9:07 pm
by chulett
Check your teammate's thread.

Posted: Sun Jul 08, 2007 9:40 pm
by ArndW
For those interested in recursion, you could write a very short factorial program for string math as follows:

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)))

Posted: Mon Jul 09, 2007 12:22 am
by ray.wurlod
But the hashed file solution will be WAY easier on memory.

Is it not said that before you can define recursion you need to define recursion?

Posted: Mon Jul 09, 2007 2:41 am
by ArndW
I didn't say it would be more efficient or faster, but it shows recursion AND string maths.
ray.wurlod wrote:...Is it not said that before you can define recursion you need to define recursion?
Only in the Department Of Redundancies Department

Factorial routine

Posted: Mon Jul 09, 2007 4:50 am
by Anoop3d
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

Posted: Mon Jul 09, 2007 8:17 am
by kcbland
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.