Count total divisors of A or B in a given range | C ++ program to count total divisors of ' a ' or ' b ' in a given range ; Utility function to find LCM of two numbers ; Function to calculate all divisors in given range ; Find LCM of a and b ; Find common divisor by using LCM ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindLCM ( int a , int b ) { return ( a * b ) / __gcd ( a , b ) ; } int rangeDivisor ( int m , int n , int a , int b ) { int lcm = FindLCM ( a , b ) ; int a_divisor = n / a - ( m - 1 ) / a ; int b_divisor = n / b - ( m - 1 ) / b ; int common_divisor = n / lcm - ( m - 1 ) / lcm ; int ans = a_divisor + b_divisor - common_divisor ; return ans ; } int main ( ) { int m = 3 , n = 11 , a = 2 , b = 3 ; cout << rangeDivisor ( m , n , a , b ) << endl ; m = 11 , n = 1000000 , a = 6 , b = 35 ; cout << rangeDivisor ( m , n , a , b ) ; return 0 ; }