I would like to calculate sum of $GCD(i, j)$ of all pairs $1 \le i \le n, 1 \le j \le m$, i.e. $\sum_{i=1}^n \sum_{j=1}^m GCD(i, j)$. Is there any faster method than calculating all those $n * m$ gcd's and summing them?
Sum of all pairs of GCD.
1
$\begingroup$
number-theory
algorithms
greatest-common-divisor
-
0Yes, but they require that you know the prime factorisations of all numbers less than $m$ or $n$. And it's still kindof tricky. – 2017-01-31
-
0Assuming that i can factorize them, how would i calculate this sum – 2017-01-31
-
0For what it's worth, the case $m=n$ is tablated at http://oeis.org/A018806. Similar question, http://math.stackexchange.com/questions/1750854/how-to-calculate-double-sum-of-gcdi-j – 2017-02-05
1 Answers
0
If you have $O(n*m)$ memory, you can store prevous GCD values and calculate GCD with $O(1)$ complexity