So first order of business is to eliminate all of these pairs. An easy way to do this is count up how many times n! is divisible by 5. Since this is always going to be smaller than the number of times n! is divisible by 2, we know that the factor 5 will be the limiting value of the (2,5) pairs.

Then as you're multiplying through, if a value is divisible by 2/5 and you haven't reached the limiting value for that number, divide the value by the appropriate value.

For example, let's take 10!:

There are two 5 prime factors of the result (5, 10).

Then as we multiply through, removing factors of 2 up to the limit:

num = 1

num = num * 2 / 2, two_count = 1

num = num * 3

num = num * 4 / 2, two_count = 2

num = num * 5 / 5, five_count = 1 (not actually necessary to keep track of five_count since every factor of 5 will be removed)

num = num * 6

num = num * 7

num = num * 8

num = num * 9

num = num * 10 / 5, five_count = 2 (not actually necessary to keep track of five_count since every factor of 5 will be removed)