Calculating Factorial in DS Routine returning junk values

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
kasgang
Premium Member
Premium Member
Posts: 9
Joined: Wed Oct 18, 2006 8:18 am

Calculating Factorial in DS Routine returning junk values

Post 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

=============================================
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

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.
kasgang
Premium Member
Premium Member
Posts: 9
Joined: Wed Oct 18, 2006 8:18 am

- Hypgeomdist in Datastage

Post 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
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post 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.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Check your teammate's thread.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post 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)))
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post 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?
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post 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
Anoop3d
Participant
Posts: 26
Joined: Wed Apr 26, 2006 10:53 pm
Location: san francisco

Factorial routine

Post 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
Ankush
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post 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.
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
Post Reply