TITLE: Counting (Number theory / Factors)
QUESTION [13 upvotes]: I'm stuck with this counting problem: I have an expression: 
$T = (N!) \times (N!) / D$ where, $D \in [1 - N!]$, i.e. $D$ takes all values from $1$ to $N!$ and I'm to count the number of points where $T$ comes out to be a whole number (a positive integer).
For example, for $N=3$:
$$ D \in \{1, 2, 3, 4, 5, 6\} $$
and $T$ comes out to be a whole number for $D \in \{1, 2, 3, 4, 6\}$, i.e. for 5 values.
I'd like to develop an efficient algorithm to count, since $N$ can be a large number.
I believe in the numerator we have the possible factors broken up as $N!$ 's and we have to start making products up till $N!$ from them and count all the distinct ones. That seems about one way to do it. Please help me unwrap it or provide any hints/pointers in the right directions to do it as efficiently as possible as $N$ here happens to be a huge value.

REPLY [6 votes]: This is quite approachable with the technique you suggest and the size of N suggested.
Let us note that this problem is virtually the same as counting all positive numbers which divide N!N! -- just add one and divide the answer by two. (The square root of N!N! is N!. For each number p less than the square root of N!N! that divide N!N! there is precisely one number q above the square root of N!N! that divides N!N!. This should be clear because pq=N!N!. The only divisor without a different match is N! itself, so we need to count it twice before dividing by two -- hence the plus one).
As for generating the number of prime factors in N!, that's also relatively easy with computers for numbers up to a million. Blitz through the numbers one through a million. For each number, do trial division (dividing that number whenever you can) until you've reached 1. Add 'two' to the count for the primes (and they will be primes) you find -- you're adding two because you've got N! twice (N!N!).
The result of this is a prime factorization of N!N!. Note that this wasn't so bad because you only had to do at most 1000 trial divisions per number (actually far less), and a thousand trial divisions for a million numbers is a billion trials. That's not fast, but it's not too slow for modern processors. (If you're writing this in C/C++/C# it'll be fast enough.)
Now to compute the count of numbers that divide N!N!... To do so, note that you can pick up to the number of two's that the prime factorization of N!N! to be factors of a divisor. Or three's. Or four's. And you can multiply them together. You can skip some prime factors, and you can choose some or all of the others.
The fundamental theorem of counting applies: we can pick the number of two's in X many ways (no twos, 1 two, 2 twos, ..., X - 1 twos), the number of three's in Y many ways, ... So the number of ways we could pick is X * Y * ...
So, your answer is ((X * Y * ...) + 1)/2