Given two positive integers X and Y, the task is to count the total numbers in range 1 to N which are divisible by X but not Y.

**Examples:**

Input:x = 2, Y = 3, N = 10

Output:4

Numbers divisible by 2 but not 3 are : 2, 4, 8, 10

Input :X = 2, Y = 4, N = 20

Output :5

Numbers divisible by 2 but not 4 are : 2, 6, 10, 14, 18

A **Simple Solution** is to count numbers divisible by X but not Y is to loop through 1 to N and counting such number which is divisible by X but not Y.

**Approach**

- For every number in range 1 to N, Increment count if the number is divisible by X but not by Y.
- Print the count.
- In range 1 to N, find
**total numbers divisible by X**and**total numbers divisible by Y**. - Also, Find
**total numbers divisible by either X or Y** - Calculate total number divisible by X but not Y as

**(total number divisible by X or Y) – (total number divisible by Y)** - Find permutation of n which is divisible by 3 but not divisible by 6
- Count natural numbers whose factorials are divisible by x but not y
- Count of numbers in range which are divisible by M and have digit D at odd places
- Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not
- Count numbers which are divisible by all the numbers from 2 to 10
- Sum of all numbers in the given range which are divisible by M
- Count integers in a range which are divisible by their euler totient value
- Split a number as sum of K numbers which are not divisible by K
- Sum of first K numbers which are not divisible by N
- Numbers that are not divisible by any number in the range [2, 10]
- Count integers in the range [A, B] that are not divisible by C and D
- Count the numbers divisible by 'M' in a given range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count numbers in a range that are divisible by all array elements
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Find the largest composite number that divides N but is strictly lesser than N

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count total numbers divisible by ` `// x but not y in range 1 to N ` `int` `countNumbers(` `int` `X, ` `int` `Y, ` `int` `N) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` `// Check if Number is divisible ` ` ` `// by x but not Y ` ` ` `// if yes, Increment count ` ` ` `if` `((i % X == 0) && (i % Y != 0)) ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `X = 2, Y = 3, N = 10; ` ` ` `cout << countNumbers(X, Y, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count total numbers divisible by ` ` ` `// x but not y in range 1 to N ` ` ` `static` `int` `countNumbers(` `int` `X, ` `int` `Y, ` `int` `N) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) { ` ` ` `// Check if Number is divisible ` ` ` `// by x but not Y ` ` ` `// if yes, Increment count ` ` ` `if` `((i % X == ` `0` `) && (i % Y != ` `0` `)) ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `X = ` `2` `, Y = ` `3` `, N = ` `10` `; ` ` ` `System.out.println(countNumbers(X, Y, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Function to count total numbers divisible ` `# by x but not y in range 1 to N ` `def` `countNumbers(X, Y, N): ` ` ` ` ` `count ` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `): ` ` ` ` ` `# Check if Number is divisible ` ` ` `# by x but not Y ` ` ` `# if yes, Increment count ` ` ` `if` `((i ` `%` `X ` `=` `=` `0` `) ` `and` `(i ` `%` `Y !` `=` `0` `)): ` ` ` `count ` `+` `=` `1` `; ` ` ` ` ` `return` `count; ` ` ` `# Driver Code ` `X ` `=` `2` `; ` `Y ` `=` `3` `; ` `N ` `=` `10` `; ` `print` `(countNumbers(X, Y, N)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to count total numbers divisible by ` ` ` `// x but not y in range 1 to N ` ` ` `static` `int` `countNumbers(` `int` `X, ` `int` `Y, ` `int` `N) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` `// Check if Number is divisible ` ` ` `// by x but not Y ` ` ` `// if yes, Increment count ` ` ` `if` `((i % X == 0) && (i % Y != 0)) ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `X = 2, Y = 3, N = 10; ` ` ` `Console.WriteLine(countNumbers(X, Y, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP implementation of above approach ` ` ` `// Function to count total numbers divisible by ` `// x but not y in range 1 to N ` `function` `countNumbers(` `$X` `, ` `$Y` `, ` `$N` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$N` `; ` `$i` `++) ` ` ` `{ ` ` ` `// Check if Number is divisible ` ` ` `// by x but not Y ` ` ` `// if yes, Increment count ` ` ` `if` `((` `$i` `% ` `$X` `== 0) && (` `$i` `% ` `$Y` `!= 0)) ` ` ` `$count` `++; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver Code ` `$X` `= 2; ` `$Y` `= 3; ` `$N` `= 10; ` `echo` `(countNumbers(` `$X` `, ` `$Y` `, ` `$N` `)); ` ` ` `// This code is contributed by Arnab Kundu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity : O(N)**

**Efficient solution:**

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count total numbers divisible by ` `// x but not y in range 1 to N ` `int` `countNumbers(` `int` `X, ` `int` `Y, ` `int` `N) ` `{ ` ` ` ` ` `// Count total number divisible by X ` ` ` `int` `divisibleByX = N / X; ` ` ` ` ` `// Count total number divisible by Y ` ` ` `int` `divisibleByY = N / Y; ` ` ` ` ` `// Count total number divisible by either X or Y ` ` ` `int` `LCM = (X * Y) / __gcd(X, Y); ` ` ` `int` `divisibleByLCM = N / LCM; ` ` ` `int` `divisibleByXorY = divisibleByX + divisibleByY ` ` ` `- divisibleByLCM; ` ` ` ` ` `// Count total numbers divisible by X but not Y ` ` ` `int` `divisibleByXnotY = divisibleByXorY ` ` ` `- divisibleByY; ` ` ` ` ` `return` `divisibleByXnotY; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `X = 2, Y = 3, N = 10; ` ` ` `cout << countNumbers(X, Y, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate GCD ` ` ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// Function to count total numbers divisible by ` ` ` `// x but not y in range 1 to N ` ` ` ` ` `static` `int` `countNumbers(` `int` `X, ` `int` `Y, ` `int` `N) ` ` ` `{ ` ` ` ` ` `// Count total number divisible by X ` ` ` `int` `divisibleByX = N / X; ` ` ` ` ` `// Count total number divisible by Y ` ` ` `int` `divisibleByY = N / Y; ` ` ` ` ` `// Count total number divisible by either X or Y ` ` ` `int` `LCM = (X * Y) / gcd(X, Y); ` ` ` `int` `divisibleByLCM = N / LCM; ` ` ` `int` `divisibleByXorY = divisibleByX + divisibleByY ` ` ` `- divisibleByLCM; ` ` ` ` ` `// Count total number divisible by X but not Y ` ` ` `int` `divisibleByXnotY = divisibleByXorY ` ` ` `- divisibleByY; ` ` ` ` ` `return` `divisibleByXnotY; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `X = ` `2` `, Y = ` `3` `, N = ` `10` `; ` ` ` `System.out.println(countNumbers(X, Y, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of above approach ` `from` `math ` `import` `gcd ` ` ` `# Function to count total numbers divisible ` `# by x but not y in range 1 to N ` `def` `countNumbers(X, Y, N): ` ` ` ` ` `# Count total number divisible by X ` ` ` `divisibleByX ` `=` `int` `(N ` `/` `X) ` ` ` ` ` `# Count total number divisible by Y ` ` ` `divisibleByY ` `=` `int` `(N ` `/` `Y) ` ` ` ` ` `# Count total number divisible ` ` ` `# by either X or Y ` ` ` `LCM ` `=` `int` `((X ` `*` `Y) ` `/` `gcd(X, Y)) ` ` ` `divisibleByLCM ` `=` `int` `(N ` `/` `LCM) ` ` ` `divisibleByXorY ` `=` `(divisibleByX ` `+` ` ` `divisibleByY ` `-` ` ` `divisibleByLCM) ` ` ` ` ` `# Count total numbers divisible by ` ` ` `# X but not Y ` ` ` `divisibleByXnotY ` `=` `(divisibleByXorY ` `-` ` ` `divisibleByY) ` ` ` ` ` `return` `divisibleByXnotY ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `X ` `=` `2` ` ` `Y ` `=` `3` ` ` `N ` `=` `10` ` ` `print` `(countNumbers(X, Y, N)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` ` ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to calculate GCD ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// Function to count total numbers divisible by ` ` ` `// x but not y in range 1 to N ` ` ` `static` `int` `countNumbers(` `int` `X, ` `int` `Y, ` `int` `N) ` ` ` `{ ` ` ` ` ` `// Count total number divisible by X ` ` ` `int` `divisibleByX = N / X; ` ` ` ` ` `// Count total number divisible by Y ` ` ` `int` `divisibleByY = N / Y; ` ` ` ` ` `// Count total number divisible by either X or Y ` ` ` `int` `LCM = (X * Y) / gcd(X, Y); ` ` ` `int` `divisibleByLCM = N / LCM; ` ` ` `int` `divisibleByXorY = divisibleByX + divisibleByY ` ` ` `- divisibleByLCM; ` ` ` ` ` `// Count total number divisible by X but not Y ` ` ` `int` `divisibleByXnotY = divisibleByXorY ` ` ` `- divisibleByY; ` ` ` ` ` `return` `divisibleByXnotY; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `X = 2, Y = 3, N = 10; ` ` ` `Console.WriteLine(countNumbers(X, Y, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `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` `); ` `} ` ` ` `// Function to count total numbers divisible ` `// by x but not y in range 1 to N ` `function` `countNumbers(` `$X` `, ` `$Y` `, ` `$N` `) ` `{ ` ` ` ` ` `// Count total number divisible by X ` ` ` `$divisibleByX` `= ` `$N` `/ ` `$X` `; ` ` ` ` ` `// Count total number divisible by Y ` ` ` `$divisibleByY` `= ` `$N` `/` `$Y` `; ` ` ` ` ` `// Count total number divisible by either X or Y ` ` ` `$LCM` `= (` `$X` `* ` `$Y` `) / __gcd(` `$X` `, ` `$Y` `); ` ` ` `$divisibleByLCM` `= ` `$N` `/ ` `$LCM` `; ` ` ` `$divisibleByXorY` `= ` `$divisibleByX` `+ ` `$divisibleByY` `- ` ` ` `$divisibleByLCM` `; ` ` ` ` ` `// Count total numbers divisible by X but not Y ` ` ` `$divisibleByXnotY` `= ` `$divisibleByXorY` `- ` ` ` `$divisibleByY` `; ` ` ` ` ` `return` `ceil` `(` `$divisibleByXnotY` `); ` `} ` ` ` `// Driver Code ` `$X` `= 2; ` `$Y` `= 3; ` `$N` `= 10; ` `echo` `countNumbers(` `$X` `, ` `$Y` `, ` `$N` `); ` ` ` `// This is code contrubted by inder_verma ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity: **O(1)

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:

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.