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:
    1. Divide N by A to get the count of numbers divisible by A in the range [1, N].
    2. Divide N by B to get the count of numbers divisible by B in the range [1, N].
    3. Divide N by A * B to get the count of numbers divisible by both A and B.
    4. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

chevron_right


.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

chevron_right


Output:

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).

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.