Permutations & Combinations for large inputs
|
05-24-2015, 05:39 PM
Post: #1
|
|||
|
|||
Permutations & Combinations for large inputs
This program will calculate permutations or combinations (nPr and nCr) for arguments that would normally overflow the FACT function when using the standard forms n!/(n-r)! and n!/(r!(n-r)!). This is a good example of using flag 25 to handle errors. The method employed here is to attempt the standard forms, setting flag 25 before each FACT, then bailing out to a routine that performs repeated multiplication and division if FACT overflowed.
The loop routine will also perform the appropriate division for COMB as the multiplication is being performed, to allow evaluating COMB for arguments that would still overflow PERM even with this method. For small inputs, execution time should be relatively quick, but larger values will take longer due to the loop operation. The below example takes 33 seconds on my unmodified 41CV. Inputs: y: n x: r XEQ PERM, or XEQ COMB Example: 234 ENTER 78 XEQ ALPHA COMB ALPHA Result: 2.6795468 63 Uses registers 20 and 21, and flags 05 and 25 (error trap). No modules required. Code: 01 LBL "PERM" |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)