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.
-
- Below is the implementation of above approach:
-
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int X, int Y, int N)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
if ((i % X == 0) && (i % Y != 0))
count++;
}
return count;
}
int main()
{
int X = 2, Y = 3, N = 10;
cout << countNumbers(X, Y, N);
return 0;
}
|
Java
class GFG {
static int countNumbers( int X, int Y, int N)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++) {
if ((i % X == 0 ) && (i % Y != 0 ))
count++;
}
return count;
}
public static void main(String[] args)
{
int X = 2 , Y = 3 , N = 10 ;
System.out.println(countNumbers(X, Y, N));
}
}
|
Python3
def countNumbers(X, Y, N):
count = 0 ;
for i in range ( 1 , N + 1 ):
if ((i % X = = 0 ) and (i % Y ! = 0 )):
count + = 1 ;
return count;
X = 2 ;
Y = 3 ;
N = 10 ;
print (countNumbers(X, Y, N));
|
C#
using System;
class GFG {
static int countNumbers( int X, int Y, int N)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
if ((i % X == 0) && (i % Y != 0))
count++;
}
return count;
}
public static void Main()
{
int X = 2, Y = 3, N = 10;
Console.WriteLine(countNumbers(X, Y, N));
}
}
|
Javascript
function countNumbers(X, Y, N) {
let count = 0;
for (let i = 1; i <= N; i++) {
if (i % X === 0 && i % Y !== 0) {
count++;
}
}
return count;
}
function main() {
const X = 2;
const Y = 3;
const N = 10;
console.log(countNumbers(X, Y, N));
}
main();
|
PHP
<?php
function countNumbers( $X , $Y , $N )
{
$count = 0;
for ( $i = 1; $i <= $N ; $i ++)
{
if (( $i % $X == 0) && ( $i % $Y != 0))
$count ++;
}
return $count ;
}
$X = 2;
$Y = 3;
$N = 10;
echo (countNumbers( $X , $Y , $N ));
?>
|
-
- Time Complexity : O(N)
- Efficient solution:
-
- 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++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int X, int Y, int N)
{
int divisibleByX = N / X;
int divisibleByY = N / Y;
int LCM = (X * Y) / __gcd(X, Y);
int divisibleByLCM = N / LCM;
int divisibleByXorY = divisibleByX + divisibleByY
- divisibleByLCM;
int divisibleByXnotY = divisibleByXorY
- divisibleByY;
return divisibleByXnotY;
}
int main()
{
int X = 2, Y = 3, N = 10;
cout << countNumbers(X, Y, N);
return 0;
}
|
Java
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int countNumbers( int X, int Y, int N)
{
int divisibleByX = N / X;
int divisibleByY = N / Y;
int LCM = (X * Y) / gcd(X, Y);
int divisibleByLCM = N / LCM;
int divisibleByXorY = divisibleByX + divisibleByY
- divisibleByLCM;
int divisibleByXnotY = divisibleByXorY
- divisibleByY;
return divisibleByXnotY;
}
public static void main(String[] args)
{
int X = 2 , Y = 3 , N = 10 ;
System.out.println(countNumbers(X, Y, N));
}
}
|
Python3
from math import gcd
def countNumbers(X, Y, N):
divisibleByX = int (N / X)
divisibleByY = int (N / Y)
LCM = int ((X * Y) / gcd(X, Y))
divisibleByLCM = int (N / LCM)
divisibleByXorY = (divisibleByX +
divisibleByY -
divisibleByLCM)
divisibleByXnotY = (divisibleByXorY -
divisibleByY)
return divisibleByXnotY
if __name__ = = '__main__' :
X = 2
Y = 3
N = 10
print (countNumbers(X, Y, N))
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int countNumbers( int X, int Y, int N)
{
int divisibleByX = N / X;
int divisibleByY = N / Y;
int LCM = (X * Y) / gcd(X, Y);
int divisibleByLCM = N / LCM;
int divisibleByXorY = divisibleByX + divisibleByY
- divisibleByLCM;
int divisibleByXnotY = divisibleByXorY
- divisibleByY;
return divisibleByXnotY;
}
public static void Main()
{
int X = 2, Y = 3, N = 10;
Console.WriteLine(countNumbers(X, Y, N));
}
}
|
Javascript
function countNumbers(X, Y, N) {
const divisibleByX = Math.floor(N / X);
const divisibleByY = Math.floor(N / Y);
const LCM = (X * Y) / gcd(X, Y);
const divisibleByLCM = Math.floor(N / LCM);
const divisibleByXorY = divisibleByX + divisibleByY - divisibleByLCM;
const divisibleByXnotY = divisibleByXorY - divisibleByY;
return divisibleByXnotY;
}
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
function main() {
const X = 2;
const Y = 3;
const N = 10;
console.log( "Count of numbers divisible by " + X + " but not by " + Y + " in the range 1 to " + N + ": " + countNumbers(X, Y, N));
}
main();
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function countNumbers( $X , $Y , $N )
{
$divisibleByX = $N / $X ;
$divisibleByY = $N / $Y ;
$LCM = ( $X * $Y ) / __gcd( $X , $Y );
$divisibleByLCM = $N / $LCM ;
$divisibleByXorY = $divisibleByX + $divisibleByY -
$divisibleByLCM ;
$divisibleByXnotY = $divisibleByXorY -
$divisibleByY ;
return ceil ( $divisibleByXnotY );
}
$X = 2;
$Y = 3;
$N = 10;
echo countNumbers( $X , $Y , $N );
?>
|
-
- Time Complexity:
- O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Oct, 2023
Like Article
Save Article