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

closed as off-topic by Jonas Meyer, Lord_Farin, Etienne, Mark Fantini, Grigory M Jan 18 at 18:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level." – Jonas Meyer, Lord_Farin, Mark Fantini, Grigory M
If this question can be reworded to fit the rules in the help center, please edit the question.

It seems very similar a question for a current programming contest. –  ILikeMath Sep 8 '13 at 19:28
Please avoid "do my homework" questions –  Etienne Jan 18 at 17:43

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

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