Sunday, April 16, 2023

Coding Challenge #53 Combinations

 // the math formula (in my dialect): x = n! / ((n-r)!r!)

// finds factorial of a number
function fac(x){
    var done = x;
    while(x > 1) done *= --x;
    return done;
}

// finds combinations given n = total number of options, and r = slots available to be filled with options.
function combo(n, r){
    return fac(n) / (fac(n-r) * fac(r));
}

console.log("Combinations for 3 elected positions, with 6 candidates = " + combo(6, 3));

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...