image thumbnail

NoverK

version 1.0.0 (3.06 KB) by Jan
Fast and accurate N over K

4 Downloads

Updated 03 Oct 2021

View License

N over K
A fast and stable computation of: n! / (k! * (n-k)!)
This is the number of combinations, if k elements are chosenout of n without
repetions and order.
c = NoverK(n, k)
INPUT:
n, k: Scalars with real integer values, floating point or integer types.
OUTPUT:
c: Result as same type as the inputs. A warning is show, if the output
exceeds the limits of specific type.
NOTES:
- Alternatives:
prod(((n-k+1):n) ./ (1:k)) 3 times slower, less accurate
nchoosek(n, k) 20 times slower, less accurate:
nchoosek(61, 17) 536830054536826 (15th digit is invalid)
NoverK(61, 17) 536830054536825
The accuracy was the main reason to write this function.
- In R2018b the processing for U/INT64 is much slower than for the other
types. Even the checking of the inputs takes much more time.
Other integer types are processed as double to increase the speed.
Tested: Matlab 2009a (no U/INT64!), 2015b(32/64), 2016b, 2018b, Win10

Cite As

Jan (2021). NoverK (https://www.mathworks.com/matlabcentral/fileexchange/100174-noverk), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018b
Compatible with R13SP2 and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!