Numbers that are not divisible by any number in the range [2, 10]
Given an integer N, The task is to find the count of all the numbers from 1 to N which are not divisible by any number in the range [2, 10].
Examples:
Input: N = 12
Output: 2
1, 11 are the only numbers in range [1, 12] which are not divisible by any number from 2 to 10
Input: N = 20
Output: 5
Approach: Total numbers from 1 to n which are not divisible by any number from 2 to 10 are equal to n minus the numbers which are divisible by some numbers from 2 to 10.
The set of numbers that are divisible by some numbers from 2 to 10 can be found as union of the set of numbers from 1 to n divisible by 2, the set of numbers divisible by 3, and so on till 10.
Note that sets of numbers divisible by 4 or 6 or 8 are subsets of the set of numbers divisible by 2, and sets of numbers divisible by 6 or 9 are subsets of the set of numbers divisible by 3. So there is no need to unite 9 sets, it is enough to unite sets for 2, 3, 5, and 7 only.
The size of the set of numbers from 1 to n divisible by 2, 3, 5, and 7 can be calculated using an inclusion-exclusion principle that says that the size of every single set should be added, the size of pairwise intersections should be subtracted, the size of all intersections of three sets should be added and so on.
The size of the set of numbers from 1 to n divisible by 2 is equal to ?n / 2?, the size of the set of numbers from 1 to n divisible by 2 and 3 is equal to ?n / (2 * 3)? and so on.
So, the formula is n – ?n / 2? – ?n / 3? – ?n / 5? – ?n / 7? + ?n / (2 * 3)] + ?n / (2 * 5)] + ?n / (2 * 7)] + ?n / (3 * 5)] + ?n / (3 * 7)] + ?n / (5 * 7)] – ?n / (2 * 3 * 5)] – ?n / (2 * 3 * 7)] – ?n / (2 * 5 * 7)] – ?n / (3 * 5 * 7)]+ ?n / (2 * 3 * 5 * 7)]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int n)
{
return n - n / 2 - n / 3 - n / 5 - n / 7
+ n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35
- n / 30 - n / 42 - n / 70 - n / 105 + n / 210;
}
int main()
{
int n = 20;
cout << countNumbers(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int countNumbers( int n)
{
return n - n / 2 - n / 3 - n / 5 - n / 7
+ n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35
- n / 30 - n / 42 - n / 70 - n / 105 + n / 210 ;
}
public static void main (String[] args)
{
int n = 20 ;
System.out.println(countNumbers(n));
}
}
|
Python3
def countNumbers(n):
return (n - n / / 2 - n / / 3 - n / / 5 - n / / 7 +
n / / 6 + n / / 10 + n / / 14 + n / / 15 +
n / / 21 + n / / 35 - n / / 30 - n / / 42 -
n / / 70 - n / / 105 + n / / 210 )
if __name__ = = '__main__' :
n = 20
print (countNumbers(n))
|
C#
using System;
class GFG
{
static int countNumbers( int n)
{
return n - n / 2 - n / 3 - n / 5 - n / 7
+ n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35
- n / 30 - n / 42 - n / 70 - n / 105 + n / 210;
}
static void Main()
{
int n = 20;
Console.WriteLine(countNumbers(n));
}
}
|
PHP
<?php
function countNumbers( $n )
{
return (int)( $n - $n / 2) - (int)( $n / 3 ) -
(int)( $n / 5 ) - (int)( $n / 7) +
(int)( $n / 6 ) + (int)( $n / 10) +
(int)( $n / 14) + (int)( $n / 15) +
(int)( $n / 21) + (int)( $n / 35) -
(int)( $n / 30) - (int)( $n / 42) -
(int)( $n / 70) - (int)( $n / 105) +
(int)( $n / 210);
}
$n = 20;
echo (countNumbers( $n ));
?>
|
Javascript
<script>
function countNumbers(n)
{
return n - parseInt(n / 2, 10) - parseInt(n / 3, 10) -
parseInt(n / 5, 10) - parseInt(n / 7, 10) +
parseInt(n / 6, 10) + parseInt(n / 10, 10) +
parseInt(n / 14, 10) + parseInt(n / 15, 10) +
parseInt(n / 21, 10) + parseInt(n / 35, 10) -
parseInt(n / 30, 10) - parseInt(n / 42, 10) -
parseInt(n / 70, 10) - parseInt(n / 105, 10) +
parseInt(n / 210, 10);
}
let n = 20;
document.write(countNumbers(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...