Marc Ermshaus’ avatar

Marc Ermshaus

Linkblog

Algorithmic Advent: 21 – Binomial coefficient (n choose k)

Published on 21 Dec 2010. Tagged with php, algorithmicadvent.

function nChooseK($n, $k)
{
    $top    = 1;
    $bottom = 1;

    for ($i = 0; $i < $k; $i++) {
        $top    *= $n - $i;
        $bottom *= $i + 1;
    }

    return $top / $bottom;
}

var_dump(nChooseK(6, 4));