Thursday, April 13, 2023

Coding Challenge #50 Permutations

 // a function to calculate the permutations given total possibilities and events to calculate for.
// for example, in a race, if we want to know total possible permutations for who comes in 1rst & 2nd, and we have 4 racers, then that'd be cp(4,2) = 12
// in order for the result to be accurate and meaningful, the eventsToCalcFor must be <= totalPossibilities
function cp(totalPossibilities, eventsToCalcFor){
    var fin = totalPossibilities;
    for(var xint = 1;xint < eventsToCalcFor;xint++){
        fin *= totalPossibilities-xint;
    }
    return fin;
}

console.log("24 racers can come into 1rst, 2nd, and 3rd places how many different ways? Answer: " + cp(24, 3));

console.log("I can organize my green, blue, and orange socks how many different ways? Answer: " + cp(3, 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 ...