Post Reply 
(HP-67/97) Combinatorics - Extended factorial, gamma, permutations, combinations
06-16-2020, 03:57 PM
Post: #2
RE: (HP-67/97) Combinatorics - Extended factorial, gamma, permutations, combinations
(06-15-2020 01:42 PM)Dave Britten Wrote:  Large combinations/permutations are generally accurate to around 5 decimal places, but accuracy worsens as the difference between x and y increases (precision is lost when subtracting ln(x!) and ln(y!) when they differ greatly in magnitude).

I think you meant precision is lost when subtracting 2 values similar in magnitude.

For nPr where n ≫ r, ln(nPr) = ln(n!) - ln((n-r)!), both nearly equal.
To avoid catastrophic cancellation, we can start with nPr ≈ n^r, and add a correction.

Reusing code from my log of probability of no repetitions (ln_nr)

Code:
function ln_nr(n,s) return 0.5*s* log1p((s-1)*(s-2-6*n)/(6*n*n)) end
function ln_nPr(n,r) return ln_nr(n,r) + r*log(n) end

Coded in HP12C (26 steps), with assumption n ≫ r

Code:
1  STO 0   ; r
2  X<>Y
3  STO 1   ; n
4  LN
5  *       ; r * ln(n)
6  RCL 0
7  2 
8  -
9  RCL 1 
10 6 
11 * 
12 STO* 1
13 -
14 RCL 1
15 /
16 RCL 0
17 1
18 -
19 *
20 1
21 +
22 LN
23 RCL 0
24 2
25 /
26 *       ; ln_nr(n,r)

Note: register X = ln_nr(n,r), Y = r*log(n). To get ln_nPr(n,r), press "+"

Example, for ln_nPr(1e6, 100)

1e6 ENTER 100
[R/S]           → X = -.004950165034, Y = 1381.551056
+                 → ln_nPr = 1381.546106
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (HP-67/97) Combinatorics - Extended factorial, gamma, permutations, combinations - Albert Chan - 06-16-2020 03:57 PM



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