Improved 19C/29C Factorial, Permutation & Combination - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: Improved 19C/29C Factorial, Permutation & Combination (/thread-14141.html) |
Improved 19C/29C Factorial, Permutation & Combination - David Hayden - 12-09-2019 08:59 PM Page 112 of the HP 19C/29C Applications Book (available on the museum thumb drive) contains programs to compute factorials, permutations and combinations. The instructions note that the programs don't handle values of n < 2. In addition, the program for combinations contains a bug. When computing COMB(N,K), if K>N, the program attempts to jump to LBL 8 at step 45 to display "Error," but instead jumps to LBL 8 at step 82 and returns whatever happened to be in register R2. This version handles n >= 0 and fixes the COMB bug. It is also shorter (65 steps vs. 84). The COMB function handles large n and small k by alternately multiplying by one value and dividing by another. This also improves accuracy. It could be improved further using some of the techniques in this excellent thread: https://www.hpmuseum.org/forum/archive/index.php?thread-3962.html Edit: See also this version which is much smaller at the expense of handling a smaller range of values in COMB(n,k): https://www.hpmuseum.org/forum/thread-14358.html USAGE: To compute N!: Enter N and press GSB 1. Example: 5 GSB 1 yields 120 To compute PERM(N,K) (the number of ways to permute N items taken K at a time): Enter N, enter K, press GSB 2. Example: 5 ENTER 2 GSB 2 yields 20. To compute COMB(N,K) (the number of ways to combine N items taken K at a time where order doesn't matter): Enter N, enter K, press GSB 3. Example 5 ENTER 2 GSB 3 yields 10. Code: 01. g LBL 1 / 15 13 01 Compute X! |