Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I am given a positive integer $N$ ($N\leq 10^9$).

How many pairs of integers $(A, B)$ exist in the range $[1,\ldots, N]$ such that $\gcd(A,B) = B$?

share|improve this question
It seems very similar a question for a current programming contest. –  ILikeMath Sep 8 '13 at 19:28

2 Answers 2

We have $\gcd(A, B)=B$ if and only if $B$ divides $A$. So the desired number of pairs would be $$ 2\left(\sum_{i=1}^{N} \lfloor N/i \rfloor\right)-N $$ We need to subtract $N$, otherwise the pairs $(i, i)$ would be counted twice. Here $\lfloor N/i \rfloor$ accounts for the number of multiples of $i$ up to $N$.

Added. The above formula counts $(A, B)$ and $(B, A)$ as solutions whenever $B$ divides $A$. But the number of ordered pairs $(A, B)$ with $B$ dividing $A$ would simply be: $$ \sum_{i=1}^{N} \lfloor N/i \rfloor $$ Here the index $i$ plays the role of $B$.

share|improve this answer
Any possible closed formula? Computing this formula with a c++ code will take too much time ( around 10 seconds ). –  Ahmedou Mousa Sep 8 '13 at 19:27
@AhmedouMousa: I don't know of a closed formula, unfortunately. –  Prism Sep 8 '13 at 19:35
good answer i just wrote a big answer assuming $N = 10^9$ and not $ N \leq 10^9 $ :( . (+1) –  what'sup Sep 8 '13 at 19:47
For $N=3$ the formula gives $2(\lfloor 3/1 \rfloor + \lfloor 3/2 \rfloor+ \lfloor 3/3 \rfloor ) - 3 = 2(3 + 1 + 1) - 3 = 7$. But I can only find 5 pairs where $gcd(a,b) = b$: $(1,1), (2,1), (3,1), (2,2), (3,3)$ What am I missing? –  Andomar Sep 8 '13 at 19:53
there are & ordered pairs in your answer @Andomar –  Ahmedou Mousa Sep 8 '13 at 19:59

There are good estimates available.

d(n)=nlogn+(2γ−1)n for very large n


share|improve this answer
what is the value for gamma ? –  user94657 Sep 13 '13 at 10:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.