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
#include <algorithm>
#include <iostream>
#include <set>
#define ll long long
using namespace std;
ll int countOfNum(ll int n, ll int a, ll int b)
{
ll int cnt_of_a, cnt_of_b, cnt_of_ab, sum;
cnt_of_a = n / a;
cnt_of_b = n / b;
sum = cnt_of_b + cnt_of_a;
cnt_of_ab = n / (a * b);
sum = sum - cnt_of_ab;
return sum;
}
ll int sumOfNum(ll int n, ll int a, ll int b)
{
ll int i;
ll int sum = 0;
set<ll int > ans;
for (i = a; i <= n; i = i + a) {
ans.insert(i);
}
for (i = b; i <= n; i = i + b) {
ans.insert(i);
}
for ( auto it = ans.begin();
it != ans.end(); it++) {
sum = sum + *it;
}
return sum;
}
int main()
{
ll int N = 88;
ll int A = 11;
ll int B = 8;
ll int count = countOfNum(N, A, B);
ll int sumofnum = sumOfNum(N, A, B);
cout << sumofnum % count << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countOfNum( int n, int a, int b)
{
int cnt_of_a, cnt_of_b, cnt_of_ab, sum;
cnt_of_a = n / a;
cnt_of_b = n / b;
sum = cnt_of_b + cnt_of_a;
cnt_of_ab = n / (a * b);
sum = sum - cnt_of_ab;
return sum;
}
static int sumOfNum( int n, int a, int b)
{
int i;
int sum = 0 ;
Set< Integer> ans = new HashSet<Integer>();
for (i = a; i <= n; i = i + a) {
ans.add(i);
}
for (i = b; i <= n; i = i + b) {
ans.add(i);
}
for (Integer it : ans) {
sum = sum + it;
}
return sum;
}
public static void main (String []args)
{
int N = 88 ;
int A = 11 ;
int B = 8 ;
int count = countOfNum(N, A, B);
int sumofnum = sumOfNum(N, A, B);
System.out.print(sumofnum % count);
}
}
|
Python3
def countOfNum(n, a, b):
cnt_of_a, cnt_of_b, cnt_of_ab, sum = 0 , 0 , 0 , 0
cnt_of_a = n / / a
cnt_of_b = n / / b
sum = cnt_of_b + cnt_of_a
cnt_of_ab = n / / (a * b)
sum = sum - cnt_of_ab
return sum
def sumOfNum(n, a, b):
i = 0
sum = 0
ans = dict ()
for i in range (a, n + 1 , a):
ans[i] = 1
for i in range (b, n + 1 , b):
ans[i] = 1
for it in ans:
sum = sum + it
return sum
if __name__ = = '__main__' :
N = 88
A = 11
B = 8
count = countOfNum(N, A, B)
sumofnum = sumOfNum(N, A, B)
print (sumofnum % count)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countOfNum( int n, int a, int b)
{
int cnt_of_a, cnt_of_b, cnt_of_ab, sum;
cnt_of_a = n / a;
cnt_of_b = n / b;
sum = cnt_of_b + cnt_of_a;
cnt_of_ab = n / (a * b);
sum = sum - cnt_of_ab;
return sum;
}
static int sumOfNum( int n, int a, int b)
{
int i;
int sum = 0;
HashSet< int > ans = new HashSet< int >();
for (i = a; i <= n; i = i + a) {
ans.Add(i);
}
for (i = b; i <= n; i = i + b) {
ans.Add(i);
}
foreach ( int it in ans) {
sum = sum + it;
}
return sum;
}
public static void Main(String []args)
{
int N = 88;
int A = 11;
int B = 8;
int count = countOfNum(N, A, B);
int sumofnum = sumOfNum(N, A, B);
Console.Write(sumofnum % count);
}
}
|
Javascript
<script>
function countOfNum(n,a,b)
{
let cnt_of_a, cnt_of_b, cnt_of_ab, sum;
cnt_of_a = Math.floor(n / a);
cnt_of_b = Math.floor(n / b);
sum = cnt_of_b + cnt_of_a;
cnt_of_ab = Math.floor(n / (a * b));
sum = sum - cnt_of_ab;
return sum;
}
function sumOfNum(n,a,b)
{
let i;
let sum = 0;
let ans = new Set();
for (i = a; i <= n; i = i + a) {
ans.add(i);
}
for (i = b; i <= n; i = i + b) {
ans.add(i);
}
for (let it of ans.values()) {
sum = sum + it;
}
return sum;
}
let N = 88;
let A = 11;
let B = 8;
let count = countOfNum(N, A, B);
let sumofnum = sumOfNum(N, A, B);
document.write(sumofnum % count);
</script>
|
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).
Auxiliary Space: O(N/A + N/B)
Last Updated :
22 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...