nPr factoral / unique combinations

by soulwire

JavaScript

// r = length of hash
// n = no. of unique possible values

function nPr( n, r ) {
    return factoral( n ) / factoral( n - r );
}

function factoral( n ) {
    var r = 1;
    for ( var i = 1; i <= n; i++ ) r = r * i;
    return r;
}

function test( s, n ) {
    document.body.innerHTML += '<p>' + s + ': ' + n + '</p>';
}

test( '4 char pin, [0-9a-z]', nPr( 26+10, 4 ) );
test( '4 dots in 3x3 grid', nPr( 9, 4 ) );