randRange(0,7) ["HATTER", "PRIOR", "BUMMED", "SASSY", "CANNON", "PLOP", "ERROR", "THAT"][INDEX] ["T","R","M","S","N","P","R","T"][INDEX] [2,2,2,3,3,2,3,2][INDEX] shuffle(WORD).join("") permUnique(PERM, REPLETTER, REPTIMES) factorial(WORD.length)/factorial(REPTIMES)

How many unique ways are there to arrange the letters in the word WORD?

Let's try building the arrangements (or permutations) letter by letter. The word is WORD.length letters long:

_.map(_.range(WORD.length), function(l){ return "_ "; }).join("")

Now, for the first blank, we have WORD.length choices of letters to put in.

After we put in the first letter, let's say it's PERM[0], we have WORD.length-1 blanks left.

PERM[0]+" "+_.map(_.range(WORD.length-1), function(l){ return "_ "; }).join("")

For the second blank, we only have WORD.length-1 choices of letters left to put in. So far, there were WORD.length \cdot WORD.length-1 unique choices we could have made.

We can continue in this fashion to put in a third letter, then a fourth, and so on. At each step, we have one fewer choice to make, until we get to the last letter, and there's only one we can put in.

Using this method, the total number of arrangements is _.map(_.range(WORD.length).reverse(), function(l){ return (++l);}).join("\\cdot") = factorial(WORD.length). Another way of writing this is WORD.length!, or WORD.length factorial, but this isn't quite the right answer.

Using the above method, we assumed that all the letters were unique. But they're not! There are REPTIMES REPLETTERs, so we're counting every permutation multiple times. So every time we have these factorial(REPTIMES) permutations:

PERM_UNIQUE.join("</br>")

We actually should have only one permutation:

PERM

Notice that we've overcounted our arrangements by REPTIMES!. This is not a coincidence! This is exactly the number of ways to permute REPTIMES objects, which we were doing with the non-unique REPLETTERs. To address this overcounting, we need to divide the number of arrangements we counted before by REPTIMES!.

When we divide the number of permutations we got by the number of times we're overcounting each permutation, we get  \dfrac{WORD.length!}{REPTIMES!} = \dfrac{factorial(WORD.length)}{factorial(REPTIMES)} = ANSWER
shuffle(["Bloopin", "Gloopin", "Prancer", "Lancer", "Quentin", "Balthazar", "Ezekiel", "Jebediah", "Rudy"], randRange(4,5)) random() > 0.5 randFromArray(NAMES, 2) FIGHTING ? factorial(NAMES.length) - factorial(NAMES.length-1)*2 : factorial(NAMES.length-1)*2

You need to put your reindeer, toSentence(NAMES), in a single-file line to pull your sleigh. However, PAIR[0] and PAIR[1] are FIGHTING ? "fighting" : "best friends", so you have to FIGHTING ? "keep them apart" : "put them next to each other", or they won't fly.

How many ways can you arrange your reindeer?

Forget about the reindeer that FIGHTING ? "can't be" : "have to be" together for a second, and let's try to figure out how many ways we can arrange the reindeer if we don't have to worry about that.

We can build our line of reindeer one by one: there are NAMES.length slots, and we have NAMES.length different reindeer we can put in the first slot.

Once we fill the first slot, we only have NAMES.length-1 reindeer left, so we only have NAMES.length-1 choices for the second slot. So far, there are NAMES.length \cdot NAMES.length-1 = NAMES.length*(NAMES.length-1) unique choices we can make.

We can continue in this way for the third reindeer, then the fourth, and so on, until we reach the last slot, where we only have one reindeer left and so we can only make one choice.

So, the total number of unique choices we could make to get to an arrangement of reindeer is _.map(_.range(NAMES.length).reverse(), function(l){ return (++l);}).join("\\cdot") = factorial(NAMES.length). Another way of writing this is NAMES.length!, or NAMES.length factorial. But we haven't thought about the two reindeer who FIGHTING ? "can't be together" : "have to be together" yet.

There are factorial(NAMES.length) different arrangements of reindeer altogether, so we just need to subtract all the arrangements where PAIR[0] and PAIR[1] are together. How many of these are there?

We can count the number of arrangements where PAIR[0] and PAIR[1] are together by treating them as one double-reindeer. Now we can use the same idea as before to come up with _.map(_.range(NAMES.length-1).reverse(), function(l){ return (++l);}).join("\\cdot") = factorial(NAMES.length-1) different arrangements. But that's not quite right.

Why? Because you can arrange the double-reindeer with PAIR[0] in front or with PAIR[1] in front, and those are different arrangements! So the actual number of arrangements with PAIR[0] and PAIR[1] together is factorial(NAMES.length-1) \cdot 2 = factorial(NAMES.length-1)*2

So, subtracting the number of arrangements where PAIR[0] and PAIR[1] are together from the total number of arrangements, we get ANSWER arrangements of reindeer where they will fly.

We can count the number of arrangements where PAIR[0] and PAIR[1] are together by treating them as one double-reindeer. Now we can use the same idea as before to come up with _.map(_.range(NAMES.length-1).reverse(), function(l){ return (++l);}).join("\\cdot") = factorial(NAMES.length-1) different arrangements. But that's not quite right.

Why? Because you can arrange the double-reindeer with PAIR[0] in front or with PAIR[1] in front, and those are different arrangements! So the actual number of arrangements with PAIR[0] and PAIR[1] together is factorial(NAMES.length-1) \cdot 2 = factorial(NAMES.length-1)*2

randRange(2,10) randRange(2,10) roundTowardsZero(100/FAC1) roundTowardsZero(100/FAC2) roundTowardsZero(100/(FAC1*FAC2))

How many numbers between 1 and 100 (inclusive) are divisible by FAC1 or FAC2?

FAC1_TIMES + FAC2_TIMES - BOTH_TIMES

There are FAC1_TIMES numbers divisible by FAC1 between 1 and 100, and FAC2_TIMES numbers divisible by FAC2 between 1 and 100. [Show me why]

One way to see this is to take  \dfrac{100}{FAC1} = FAC1_TIMES
One way to see this is to take  \dfrac{100}{FAC1}, which is between  FAC1_TIMES and FAC1_TIMES+1. So, starting at 0 and adding FAC1 at each step, you have to take between FAC1_TIMES and FAC1_TIMES+1 steps to get to 100. Since if you take a fraction of a step you won't get to a number divisible by FAC1, you'll hit FAC1_TIMES numbers divisible by FAC1 before you get to above 100. So, there are FAC1_TIMES numbers between 1 and 100 divisible by FAC1.

Using similar logic for the second factor, we see that  \dfrac{100}{FAC2} = FAC2_TIMES
Using similar logic for the second factor, take  \dfrac{100}{FAC2}, which is between  FAC2_TIMES and FAC2_TIMES+1. So, starting at 0 and adding FAC2 at each step, you have to take between FAC2_TIMES and FAC2_TIMES+1 steps to get to 100. Since if you take a fraction of a step you won't get to a number divisible by FAC2, you'll hit FAC2_TIMES numbers divisible by FAC2 before you get to above 100. So, there are FAC2_TIMES numbers between 1 and 100 divisible by FAC2.

So, you might think there are FAC1_TIMES + FAC2_TIMES = FAC1_TIMES+FAC2_TIMES numbers divisible by one or the other, but this is overcounting something.

We're counting every number which is divisible by both FAC1 and FAC2 twice. So, for example, FAC1*FAC2 is counted once as a number divisible by FAC1, and then again as a number divisible by FAC2.

So, we need to count how many numbers are divisible by both FAC1 and FAC2 and subtract this from what we had before.

Being divisible by both FAC1 and FAC2 is the same thing as being divisible by FAC1*FAC2, so there plural("is",BOTH_TIMES) BOTH_TIMES plural("number",BOTH_TIMES) between 1 and 100 divisible by both.

Subtracting, there are FAC1_TIMES + FAC2_TIMES - BOTH_TIMES = FAC1_TIMES + FAC2_TIMES - BOTH_TIMES numbers divisible by FAC1 or FAC2.