Q:

Combinatorics in SQL: "Choose" Function

Using PLSQL Developer and looking for a simple function for n choose k.  I just can't find it via Google, since I keep getting information about CASE when I search "Choose".  Thanks!

A:

Based on the knowledge found on Wikipedia:
function combination(n in integer, k in integer)
    return integer
is
    l_result integer;
begin
    for i in 1..k loop
        l_result := l_result * (n-i+1) / k;
    end loop;
    return l_result;
end;
/

Enjoy.

