Find and Count total factors of co-prime A or B in a given range 1 to N
Given three integers N, A, B, the task is to find the remainder when the sum of integers which are divisible by either A or B in the range [1, N] is divided by the number of integers in this range.
Note: The numbers A and B are co-primes.
Examples:
Input: N = 88, A = 11, B = 8
Output: 8
Explanation:
There are a total of 18 numbers in the range [1, 88] which are divisible by either 8 or 11. They are:
{ 8, 11, 16, 22, 24, 32, 33, 40, 44, 48, 55, 56, 64, 66, 72, 77, 80, 88 }. Therefore, the sum of these numbers is 836. Therefore, 836 % 18 = 8.Input: N = 100, A = 7, B = 19
Output: 13
Explanation:
There are a total of 19 numbers in the range [1, 100] which are divisible by either 7 or 19. They are:
{ 7, 14, 19, 21, 28, 35, 38, 42, 49, 56, 57, 63, 70, 76, 77, 84, 91, 95, 98 }. Therefore, the sum of these numbers is 1020. Therefore, 1020 % 19 = 13.
Naive Approach: The naive approach is to run a loop from 1 to N and count all the numbers which are divisible by either A or B while simultaneously adding those numbers in a variable to find its sum.
Time Complexity: O(N)
Efficient Approach: Efficient approach is to use the division method.
- By using the division method, the count of the numbers which are divisible either by A or B can be found in the constant time. The idea is to:
- Divide N by A to get the count of numbers divisible by A in the range [1, N].
- Divide N by B to get the count of numbers divisible by B in the range [1, N].
- Divide N by A * B to get the count of numbers divisible by both A and B.
- Add the values obtained in step 1 and step 2 and subtract the value obtained in step 3 to remove the numbers which have been counted twice.
- Since we are even interested in finding the numbers which are divisible in this range, the idea is to reduce the number of times the conditions are checked by the following way:
- Instead of completely relying on one loop, we can use two loops.
- One loop is to find the numbers divisible by A. Instead of incrementing the values by 1, we start the loop from A and increment it by A. This reduces the number of comparisons drastically.
- Similarly, another loop is used to find the numbers divisible by B.
- Since again, there might be repetitions in the numbers, the numbers are stored in a set so that there are no repetitions.
- Once the count and sum of the numbers are found, then directly modulo operation can be applied to compute the final answer.
Below is the implementation of the above approach:
CPP
.
Java
Python3
C#
8
Time Complexity Analysis:
- The time taken to run the for loop to find the numbers which are divisible by A is O(N / A).
- The time taken to run the for loop to find the numbers which are divisible by B is O(N / B).
- Therefore, overall time complexity is O(N / A) + O(N / B).
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Count numbers from range whose prime factors are only 2 and 3
- Count total divisors of A or B in a given range
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Finding a Non Transitive Coprime Triplet in a Range
- Find the row whose product has maximum count of prime factors
- Find the total count of numbers up to N digits in a given base B
- Find number of factors of N when location of its two factors whose product is N is given
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Sum of all even factors of numbers in the range [l, r]
- Sum of all odd factors of numbers in the range [l, r]
- Number of elements with even factors in the given range
- Number of elements with odd factors in given range
- Program to find count of numbers having odd number of divisors in given range
- K-Primes (Numbers with k prime factors) in a range
- Total numbers with no repeated digits in a range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

