Permutations & Combinations for large inputs
|
05-26-2015, 09:26 AM
(This post was last modified: 05-26-2015 09:51 PM by Dieter.)
Post: #2
|
|||
|
|||
RE: Permutations & Combinations for large inputs
(05-24-2015 05:39 PM)Dave Britten Wrote: 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. Dave, your method requires just one single overflow test: If n! does not overflow, the smaller values (n–r)! and r! won't either. So lines 17,19 and 20 resp. 25, 27 and 28 can be removed. They should be removed because a set flag 25 may cover other errors, e.g. non-integer or negative arguments. Just for comparison, here is another version that does not require any data registers and runs faster due to two separate loops and some further improvements within these. For instance two counters are used that are set up before the loops start so that no additional arithmetics within the loops are required. Both loops can be combined to one, but in this case especially nPr would run slower. The program also uses the property Comb(n,r) = Comb(n, n–r), which substantially speeds up the program for r > n/2. Flag 25 is not used – checking whether n>69 will do. Code: 01 LBL"NPR" // Permutations I sincerely hope you do not mind my suggestion – it includes some basic ideas that may be helpful for other programs as well. Edit: program modified to handle r=0 (and n–r=0 for nCr) correctly. Dieter |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)