I've been stuck in this problem for a time, now. The problem is as follows:
For any integer $n \gt0$ and prime number $p,$ define $\nu_p(n)$ as the greatest integer $r$ such that $p^r$ divides $n$.
Define $$D(n, m) = \sum_{p \text{ prime}} \left| \nu_p(n) - \nu_p(m)\right|.$$ For example, $D(14,24) = 4$.
Furthermore, define $$S(N) = \sum_{1 \le n, m \le N} D(n, m).$$ You are given $S(10) = 210$ and $S(10^2) = 37018$.
Find $S(10^{12})$. Give your answer modulo $1\,000\,000\,007$.
I have written the simpler forms of these functions, and I get $D(14,24) = 4$, as well as $S(10) = 210$ and $S(100) = 37018$. Furthermore, I have ''discovered'' the relationship
\begin{equation} \sum_{n=1}^N \nu_p(n) = \sum_{k=1}^{p^k \lt N} \Bigl \lfloor\frac{N}{p^k} \Bigr \rfloor \end{equation}
But it didn't help me, since the absolute value was taken from the expression and I was not able to generalize. Nonetheless, I could not find a way to approach it without creating a sieve of the the prime numbers bellow $10^{12}$. I believe that in order to solve this problem, one would need to have some deeper knowledge of number theory that I have. I would like some insights to solve this problem (I am not asking for a solution, just some mathematical hints to approach it).
As with every Project Euler problem, you should start by inspecting what is really going on inside the function. In this specific case, the numbers $n,m$ don't actually matter - what you sum is the exponent difference of primes. You have to consider every prime $p$ up to $N$, and note its occurences for every exponent $e$ such that $p^e \le N$. Let's take a closer look at the case of $N=10$. As I've said, I will rather look at the primes than on the numbers $n,m$ themselves. Looking at $n,m$ themselves is highly misleading, which is my first tip to you and the first onion shell you have to remove off of this problem in order to solve it.
For $p=2$:
There are $5$ numbers in which $0$ is the highest power of $2$ in their prime decomposition: $1,3,5,7,9$
There are $3$ numbers in which $1$ is the highest power of $2$ in their prime decomposition: $2,6,10$
There is $1$ number in which $2$ is the highest power of $2$ in its prime decomposition: $4$
There is $1$ number in which $3$ is the highest power of $2$ in its prime decomposition: $8$
It is of curicial importance not to forget about the $0$'th exponent that is also possible. As a verification, there are obviously $10$ numbers in the range $[1,10]$, and here we have listed them all, exactly 10 numbers - $5 + 3 + 1+1 = 10$. It is easy enough to derive a "formula" to find how many numbers exist in which $p^e$ appears for each prime $p$ and each exponent $e$ in a given range $[1,N]$. I will leave it for you to deduce.
In a similiar manner, we find for the other primes:
For $p=3$:
There are $7$ numbers in which $0$ is the highest power of $3$ in their prime decomposition: $1,2,4,5,7,8,10$
There are $2$ numbers in which $1$ is the highest power of $3$ in their prime decomposition: $3,6$
There is $1$ number in which $2$ is the highest power of $3$ in its prime decomposition: $9$
For $p=5$:
There are $8$ numbers in which $0$ is the highest power of $5$ in their prime decomposition: $1,2,3,4,6,7,8,9$
There are $2$ numbers in which $1$ is the highest power of $5$ in their prime decomposition: $5,10$
For $p=7$:
There are $9$ numbers in which $0$ is the highest power of $7$ in their prime decomposition: $1,2,3,4,5,6,8,9,10$
There is $1$ number in which $1$ is the highest power of $7$ in their prime decomposition: $7$
Now, for each prime seperately, we match every group with all the others. Take 2 for example. There are $5$ numbers with $2^0$ in their prime decomposition. Similiarly, there are $3$ numbers with $2^1$ in their prime decomposition. We can match every number from the first group with a number from the second group, to get an exponent difference of exactly $1$. And there are $3\cdot 5 = 15$ such matchings, each contributing exponent difference of $1$ to the final sum. Because the problem does not state that $n<m$ or otherwise, then the pairs $(3,10) , (10,3)$ are to be counted seperately. Therefore the contribution I have just described has to be multiplied by 2, resulting in total contribution to the final sum of $30$. It is easier to find the "half-contribution" of all group matchings and multiply by 2 in the end.
Similiarly, for each prime, we match 2 groups in turns to get the final sum. I will now quickly sketch this to see where this $210$ comes from:
For $p = 2$:
As per my example, $5 \cdot 3\cdot \lvert(0 - 1)\rvert = 15$ (1st and 2nd group)
$5 \cdot 1\cdot \lvert(0 - 2)\rvert = 10$ (1st and 3rd group)
$5 \cdot 1\cdot \lvert(0 - 3)\rvert = 15$ (1st and 4th group)
$3 \cdot 1\cdot \lvert(1 - 2)\rvert = 3$ (2nd and 3rd group)
$3 \cdot 1\cdot \lvert(1 - 3)\rvert = 6$ (2nd and 4th group)
$1 \cdot 1\cdot \lvert(2 - 3)\rvert = 1$ (3rd and 4th group)
For $p = 3$:
$7 \cdot 2\cdot \lvert(0 - 1)\rvert = 14$ (1st and 2nd group)
$7 \cdot 1\cdot \lvert(0 - 2)\rvert = 14$ (1st and 3rd group)
$2 \cdot 1\cdot \lvert(1 - 2)\rvert = 2$ (2nd and 3rd group)
For $p = 5$:
$8 \cdot 2\cdot \lvert(0 - 1)\rvert = 16$ (1st and 2nd group)
For $p = 7$:
$9 \cdot 1\cdot \lvert(0 - 1)\rvert = 9$ (1st and 2nd group)
Summing it all up, we find:
$$S(10^2) = 2\cdot [(15 + 10 + 15 + 3 + 6 + 1) + (14 + 14 + 2) + (16) + (9)]=2\cdot[(50)+(30)+(16)+(9)] = 2 \cdot 105 = 210$$
(and not forgetting to multiply the sum by 2 in the end, because the order in which we choose the groups matters! i.e, "1st and 2nd" group is different than "2nd and 1st" group, but they both have the same contribution)
So, $S(10^2) = 210$ indeed. I encourage you to experiment with it a little, and with a computer program you will find more key observations.
Some hints and tips towards solving this problem:
Additionally, here are some more test values for you to compare your algorithm and know you're on the right path:
$S(10^1) = 210$
$S(10^2) = 37018$
$S(10^3) = 4654406$
$S(10^4) = 529398010$
$S(10^5) = 57689386540$
$S(10^6) = 6149855192622$
$S(10^7) = 646886370938854$
$S(10^8) = 67436388950708040$
$S(10^9) = 6985053526514689852$
$S(10^{10}) = 720037271618744270714$
Bare in mind that $S(10^9)$ is as far as a "dumb"-brute-force may get you. Getting beyond that requires further research, and clever as well as efficient implementations of algorithms I have described above as tips. I have included $S(10^{10})$ for you to know you have indeed managed to create such implementations. As a side note, my algorithm computes $S(10^{12})$ in less than a second, so it is indeed possible. All you have to do to solve it is sketch the inner machinery of this function as I have done thoroughly, and explore the behaviour of the primes.