Post Reply 
Permutations & Combinations for large inputs
05-26-2015, 12:22 PM
Post: #4
RE: Permutations & Combinations for large inputs
Common pitfalls for a COMB routine:
- C(8,3) calculated as 8/3*7/2*6 returns 56.00000001
solution : calculate it as 8/1*7/2*6/3, guaranteeing integers all along, but then...:
- C(335,167) overflows while the result is < 1e100
solution: calculate 0.001*n/1*(n-1)/2*(n-2)/3... *1000
1e3 is large enough because we are looking for the largest p for which C(n,p)<1e100,
and that is 168 (C(336,168) = 6e99)
The largest intermediate number formed is p*C(n,p).
- C(n,0) = 1
- C(n,n-1) = C(n,1)
solution: calculate C(n,min(p,n-p))

The following routine avoids these, but may return slightly inaccurate
results > 1e10 due to roundoff. Nothing to be done about that.

Code:
                L       X       Y       Z       T
00 { 45-Byte Prgm }
01>LBL"COMB"            p       n       z
02 ST- Y                p       n-p     z       -
03 X>Y?
04 X<>Y                 m       n-m     z       -
05 +            m       n       z       -       -
06 1 E-3        m       ,001    n       z       -
07 STx L        0,m     ,001    n       z       -
08 X<>Y         0,m     n       ,001    z       -
09 ISG L        1,m     n       ,001    z       -
10 X=0?            Always False Filler
11 GTO 00
12>LBL 02       1,m     n       10^-3   z       -
13 STx Y
14 LASTX
15 INT
16 ST/ Z
17 Rv
18 DSE X
19 ISG L
20 GTO 02
21>LBL 00        -        -        c/10^3        z        -
22 Rv
23 1 E3
24 x
25 END

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Permutations & Combinations for large inputs - Werner - 05-26-2015 12:22 PM



User(s) browsing this thread: 1 Guest(s)