Given a range from L to R and every Xth tile is painted black and every Yth tile is painted white in that range from L to R. If a tile is painted both white and black, then it is considered to be painted grey. The task is to find the number of tiles that are colored grey in range L to R (both inclusive).

**Examples:**

Input: X = 2, Y = 3, L = 6, R = 18 Output: 3 The grey coloured tiles are numbered 6, 12, 18 Input: X = 1, Y = 4, L = 5, R = 10 Output: 1 The only grey coloured tile is 8.

**Approach:** Since every multiple of X is black and every multiple of Y is white. Any tile which is a multiple of both X and Y would be grey. The terms that are divisible by both X and Y are the terms that are divisible by the lcm of X and Y.

Lcm can be found out using the following formula:

lcm = (x*y) / gcd(x, y)

GCD can be computed in logn time using Euclid’s algorithm. The number of multiples of lcm in range L to R can be found by using a common trick of:

count(L, R) = count(R) - count(L-1)

Number of terms divisible by K less than N is:

floor(N/K)

Below is the implementation to find the number of grey tiles:

## C++

`// C++ implementation to find the number of ` `// grey tiles ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the numbe ro fgrey tiles ` `int` `findTileCount(` `int` `x, ` `int` `y, ` `int` `l, ` `int` `r) ` `{ ` ` ` `int` `lcm = (x * y) / __gcd(x, y); ` ` ` ` ` `// Number multiple of lcm less than L ` ` ` `int` `countl = (l - 1) / lcm; ` ` ` ` ` `// Number of multiples of lcm less than R+1 ` ` ` `int` `countr = r / lcm; ` ` ` `return` `countr - countl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 2, y = 3, l = 6, r = 18; ` ` ` `cout << findTileCount(x, y, l, r); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// number of grey tiles ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to count the number ` `// of grey tiles ` `static` `int` `findTileCount(` `int` `x, ` `int` `y, ` ` ` `int` `l, ` `int` `r) ` `{ ` ` ` `int` `lcm = (x * y) / __gcd(x, y); ` ` ` ` ` `// Number multiple of lcm less than L ` ` ` `int` `countl = (l - ` `1` `) / lcm; ` ` ` ` ` `// Number of multiples of ` ` ` `// lcm less than R+1 ` ` ` `int` `countr = r / lcm; ` ` ` `return` `countr - countl; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Everything divides 0 ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` ` ` `// base case ` ` ` `if` `(a == b) ` ` ` `return` `a; ` ` ` ` ` `// a is greater ` ` ` `if` `(a > b) ` ` ` `return` `__gcd(a - b, b); ` ` ` ` ` `return` `__gcd(a, b - a); ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `x = ` `2` `, y = ` `3` `, l = ` `6` `, r = ` `18` `; ` ` ` `System.out.println(findTileCount(x, y, l, r)); ` `} ` `} ` ` ` `// This code is contributed ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the number of ` `# grey tiles ` ` ` `# from math lib import gcd method ` `from` `math ` `import` `gcd ` ` ` `# Function to count the numbe of grey tiles ` `def` `findTileCount(x, y, l, r) : ` ` ` ` ` `lcm ` `=` `(x ` `*` `y) ` `/` `/` `gcd(x, y) ` ` ` ` ` `# Number multiple of lcm less than L ` ` ` `count1 ` `=` `(l ` `-` `1` `) ` `/` `/` `lcm ` ` ` ` ` `# Number of multiples of lcm less than R+1 ` ` ` `countr ` `=` `r ` `/` `/` `lcm ` ` ` ` ` `return` `countr ` `-` `count1 ` ` ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `x, y, l, r ` `=` `2` `, ` `3` `, ` `6` `, ` `18` ` ` `print` `(findTileCount(x, y, l, r)) ` ` ` `# This code is contributed by ` `# ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// number of grey tiles ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count the number ` `// of grey tiles ` `static` `int` `findTileCount(` `int` `x, ` `int` `y, ` ` ` `int` `l, ` `int` `r) ` `{ ` ` ` `int` `lcm = (x * y) / __gcd(x, y); ` ` ` ` ` `// Number multiple of lcm less than L ` ` ` `int` `countl = (l - 1) / lcm; ` ` ` ` ` `// Number of multiples of ` ` ` `// lcm less than R+1 ` ` ` `int` `countr = r / lcm; ` ` ` `return` `countr - countl; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Everything divides 0 ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` ` ` `// base case ` ` ` `if` `(a == b) ` ` ` `return` `a; ` ` ` ` ` `// a is greater ` ` ` `if` `(a > b) ` ` ` `return` `__gcd(a - b, b); ` ` ` ` ` `return` `__gcd(a, b - a); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `x = 2, y = 3, l = 6, r = 18; ` ` ` `Console.Write(findTileCount(x, y, l, r)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Kirti_Mangal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to find the ` `// number of grey tiles ` ` ` `// Function to count the number ` `// of grey tiles ` `function` `findTileCount(` `$x` `, ` `$y` `, ` `$l` `, ` `$r` `) ` `{ ` ` ` `$lcm` `= (int)((` `$x` `* ` `$y` `) / __gcd(` `$x` `, ` `$y` `)); ` ` ` ` ` `// Number multiple of lcm less than L ` ` ` `$countl` `= (int)((` `$l` `- 1) / ` `$lcm` `); ` ` ` ` ` `// Number of multiples of ` ` ` `// lcm less than R+1 ` ` ` `$countr` `= (int)(` `$r` `/ ` `$lcm` `); ` ` ` `return` `$countr` `- ` `$countl` `; ` `} ` ` ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// Everything divides 0 ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` ` ` `// base case ` ` ` `if` `(` `$a` `== ` `$b` `) ` ` ` `return` `$a` `; ` ` ` ` ` `// a is greater ` ` ` `if` `(` `$a` `> ` `$b` `) ` ` ` `return` `__gcd(` `$a` `- ` `$b` `, ` `$b` `); ` ` ` ` ` `return` `__gcd(` `$a` `, ` `$b` `- ` `$a` `); ` `} ` ` ` `// Driver code ` `$x` `= 2; ` `$y` `= 3; ` `$l` `= 6; ` `$r` `= 18; ` `echo` `findTileCount(` `$x` `, ` `$y` `, ` `$l` `, ` `$r` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity: ** O(log(x*y))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count common elements in two arrays containing multiples of N and M
- Count common prime factors of two numbers
- Sum of multiples of Array elements within a given range [L, R]
- Count Odd and Even numbers in a range from L to R
- Count of numbers having only 1 set bit in the range [0, n]
- Count of numbers from range [L, R] that end with any of the given digits
- Count the numbers divisible by 'M' in a given range
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of numbers with all digits same in a given range
- Count factorial numbers in a given range
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count numbers with unit digit k in given range
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count all the numbers in a range with smallest factor as K
- Count of Double Prime numbers in a given range L to R

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.