Count numbers in range 1 to N which are divisible by X but not by Y
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, 10Input : 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)
Below is the implementation of above approach:
C++
// C++ implementation of above approach#include <bits/stdc++.h>usingnamespacestd;// Function to count total numbers divisible by// x but not y in range 1 to NintcountNumbers(intX,intY,intN){intcount = 0;for(inti = 1; i <= N; i++) {// Check if Number is divisible// by x but not Y// if yes, Increment countif((i % X == 0) && (i % Y != 0))count++;}returncount;}// Driver Codeintmain(){intX = 2, Y = 3, N = 10;cout << countNumbers(X, Y, N);return0;}Java
// Java implementation of above approachclassGFG {// Function to count total numbers divisible by// x but not y in range 1 to NstaticintcountNumbers(intX,intY,intN){intcount =0;for(inti =1; i <= N; i++) {// Check if Number is divisible// by x but not Y// if yes, Increment countif((i % X ==0) && (i % Y !=0))count++;}returncount;}// Driver Codepublicstaticvoidmain(String[] args){intX =2, Y =3, N =10;System.out.println(countNumbers(X, Y, N));}}Python3
# Python3 implementation of above approach# Function to count total numbers divisible# by x but not y in range 1 to NdefcountNumbers(X, Y, N):count=0;foriinrange(1, N+1):# Check if Number is divisible# by x but not Y# if yes, Increment countif((i%X==0)and(i%Y !=0)):count+=1;returncount;# Driver CodeX=2;Y=3;N=10;print(countNumbers(X, Y, N));# This code is contributed by mitsC#
// C# implementation of the above approachusingSystem;classGFG {// Function to count total numbers divisible by// x but not y in range 1 to NstaticintcountNumbers(intX,intY,intN){intcount = 0;for(inti = 1; i <= N; i++) {// Check if Number is divisible// by x but not Y// if yes, Increment countif((i % X == 0) && (i % Y != 0))count++;}returncount;}// Driver CodepublicstaticvoidMain(){intX = 2, Y = 3, N = 10;Console.WriteLine(countNumbers(X, Y, N));}}PHP
<?php//PHP implementation of above approach// Function to count total numbers divisible by// x but not y in range 1 to NfunctioncountNumbers($X,$Y,$N){$count= 0;for($i= 1;$i<=$N;$i++){// Check if Number is divisible// by x but not Y// if yes, Increment countif(($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?>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>usingnamespacestd;// Function to count total numbers divisible by// x but not y in range 1 to NintcountNumbers(intX,intY,intN){// Count total number divisible by XintdivisibleByX = N / X;// Count total number divisible by YintdivisibleByY = N / Y;// Count total number divisible by either X or YintLCM = (X * Y) / __gcd(X, Y);intdivisibleByLCM = N / LCM;intdivisibleByXorY = divisibleByX + divisibleByY- divisibleByLCM;// Count total numbers divisible by X but not YintdivisibleByXnotY = divisibleByXorY- divisibleByY;returndivisibleByXnotY;}// Driver Codeintmain(){intX = 2, Y = 3, N = 10;cout << countNumbers(X, Y, N);return0;}Java
// Java implementation of above approachclassGFG {// Function to calculate GCDstaticintgcd(inta,intb){if(b ==0)returna;returngcd(b, a % b);}// Function to count total numbers divisible by// x but not y in range 1 to NstaticintcountNumbers(intX,intY,intN){// Count total number divisible by XintdivisibleByX = N / X;// Count total number divisible by YintdivisibleByY = N / Y;// Count total number divisible by either X or YintLCM = (X * Y) / gcd(X, Y);intdivisibleByLCM = N / LCM;intdivisibleByXorY = divisibleByX + divisibleByY- divisibleByLCM;// Count total number divisible by X but not YintdivisibleByXnotY = divisibleByXorY- divisibleByY;returndivisibleByXnotY;}// Driver Codepublicstaticvoidmain(String[] args){intX =2, Y =3, N =10;System.out.println(countNumbers(X, Y, N));}}Python3
# Python 3 implementation of above approachfrommathimportgcd# Function to count total numbers divisible# by x but not y in range 1 to NdefcountNumbers(X, Y, N):# Count total number divisible by XdivisibleByX=int(N/X)# Count total number divisible by YdivisibleByY=int(N/Y)# Count total number divisible# by either X or YLCM=int((X*Y)/gcd(X, Y))divisibleByLCM=int(N/LCM)divisibleByXorY=(divisibleByX+divisibleByY-divisibleByLCM)# Count total numbers divisible by# X but not YdivisibleByXnotY=(divisibleByXorY-divisibleByY)returndivisibleByXnotY# Driver Codeif__name__=='__main__':X=2Y=3N=10print(countNumbers(X, Y, N))# This code is contributed by# Surendra_GangwarC#
// C# implementation of above approachusingSystem;classGFG {// Function to calculate GCDstaticintgcd(inta,intb){if(b == 0)returna;returngcd(b, a % b);}// Function to count total numbers divisible by// x but not y in range 1 to NstaticintcountNumbers(intX,intY,intN){// Count total number divisible by XintdivisibleByX = N / X;// Count total number divisible by YintdivisibleByY = N / Y;// Count total number divisible by either X or YintLCM = (X * Y) / gcd(X, Y);intdivisibleByLCM = N / LCM;intdivisibleByXorY = divisibleByX + divisibleByY- divisibleByLCM;// Count total number divisible by X but not YintdivisibleByXnotY = divisibleByXorY- divisibleByY;returndivisibleByXnotY;}// Driver CodepublicstaticvoidMain(){intX = 2, Y = 3, N = 10;Console.WriteLine(countNumbers(X, Y, N));}}PHP
<?php// PHP implementation of above approachfunction__gcd($a,$b){// Everything divides 0if($a== 0)return$b;if($b== 0)return$a;// base caseif($a==$b)return$a;// a is greaterif($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 NfunctioncountNumbers($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;returnceil($divisibleByXnotY);}// Driver Code$X= 2;$Y= 3;$N= 10;echocountNumbers($X,$Y,$N);// This is code contrubted by inder_verma?>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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


