Given a number n. We need find the number of ordered pairs of a and b such gcd(a, b) is b itself
Input : n = 2 Output : 3 (1, 1) (2, 2) and (2, 1) Input : n = 3 Output : 5 (1, 1) (2, 2) (3, 3) (2, 1) and (3, 1)
Naive approach : gcd(a, b) = b means b is a factor of a. So total number of pairs will be equal to sum of divisors for each a = 1 to n. Please refer find all divisors of a natural number for implementation.
Efficient approach : gcd(a, b) = b means that a is a multiple of b. So total number of pairs will be sum of number of multiples of each b (where b varies from 1 to n) which are less than or equal to n.
For a number i, number of multiples of i is less than or equal to floor(n/i). So what we need to do is just sum the floor(n/i) for each i = 1 to n and print it. But more optimizations can be done. floor(n/i) can have atmost 2*sqrt(n) values for i >= sqrt(n). floor(n/i) can vary from 1 to sqrt(n) and similarly for i = 1 to sqrt(n) floor(n/i) can have values from 1 to sqrt(n). So total of 2*sqrt(n) distinct values
let floor(n/i) = k k <= n/i < k + 1 n/k+1 < i <= n/k floor(n/k+1) < i <= floor(n/k) Thus for given k the largest value of i for which the floor(n/i) = k is floor(n/k) and all the set of i for which the floor(n/i) = k are consecutive
1 3 5
This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]
- Given two arrays count all pairs whose sum is an odd number
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count pairs with Bitwise AND as ODD number
- Count pairs with sum as a prime number and less than n
- Count number of pairs with positive sum in an array
- Count number of ordered pairs with Even and Odd Sums
- Count all pairs of divisors of a number N whose sum is coprime with N
- Count number of ordered pairs with Even and Odd Product
- Count number of pairs in array having sum divisible by K | SET 2
- Queries to count the number of unordered co-prime pairs from 1 to N
- Count pairs of natural numbers with GCD equal to given number
- Count number of pairs of lines intersecting at a Point
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count pairs with given sum | Set 2
- Count pairs with Odd XOR
- Count Pairs from two arrays with even sum
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count of odd and even sum pairs in an array