Number that are not divisible by any number from 2 to 10

Last Updated : 21 Jul, 2026

Given a positive integer n, how many integers in the range [1, n] are not divisible by any of the numbers from 2 to 10.

Examples:

Input: n = 11
Output: 2
Explanation: The numbers are 1 and 11.

Input: n = 2
Output: 1
Explanation: The only number is 1.

Input: n = 30
Output: 7
Explanation: The numbers are 1, 11, 13, 17, 19, 23 and 29

Try It Yourself
redirect icon

[Naive Approach] Check Every Number - O(n) Time and O(1) Space

Iterate through all integers from 1 to n. For each number, check whether it is divisible by any number from 2 to 10. If it is not divisible by any of them, increment the answer. Finally, return the count.

C++
#include <iostream>
using namespace std;

int countNonDivisible(int n) {
    int res = 0;

    for (int k = 1; k <= n; k++) {
        bool valid = true;

        for (int d = 2; d <= 10; d++) {
            if (k % d == 0) {
                valid = false;
                break;
            }
        }

        if (valid) {
            res++;
        }
    }

    return res;
}

int main() {
    cout << countNonDivisible(11) << endl;
    cout << countNonDivisible(2) << endl;

    return 0;
}
Java
class GFG {
    static int countNonDivisible(int n) {
        int res = 0;

        for (int k = 1; k <= n; k++) {
            boolean valid = true;

            for (int d = 2; d <= 10; d++) {
                if (k % d == 0) {
                    valid = false;
                    break;
                }
            }

            if (valid) {
                res++;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        System.out.println(countNonDivisible(11));
        System.out.println(countNonDivisible(2));
    }
}
Python
def countNonDivisible(n):
    res = 0

    for k in range(1, n + 1):
        valid = True

        for d in range(2, 11):
            if k % d == 0:
                valid = False
                break

        if valid:
            res += 1

    return res

if __name__ == "__main__":
    print(countNonDivisible(11))
    print(countNonDivisible(2))
C#
using System;

class GFG {
    static int countNonDivisible(int n) {
        int res = 0;

        for (int k = 1; k <= n; k++) {
            bool valid = true;

            for (int d = 2; d <= 10; d++) {
                if (k % d == 0) {
                    valid = false;
                    break;
                }
            }

            if (valid) {
                res++;
            }
        }

        return res;
    }

    static void Main() {
        Console.WriteLine(countNonDivisible(11));
        Console.WriteLine(countNonDivisible(2));
    }
}
JavaScript
function countNonDivisible(n) {
    let res = 0;

    for (let k = 1; k <= n; k++) {
        let valid = true;

        for (let d = 2; d <= 10; d++) {
            if (k % d === 0) {
                valid = false;
                break;
            }
        }

        if (valid) {
            res++;
        }
    }

    return res;
}

// Driver Code
console.log(countNonDivisible(11));
console.log(countNonDivisible(2));

Output
2
1

[Expected Approach] Inclusion-Exclusion Principle - O(1) Time and O(1) Space

Any number divisible by 4, 6, 8, 9 or 10 is already divisible by at least one of the prime numbers 2, 3, 5 or 7. Therefore, we only need to count numbers divisible by 2, 3, 5 or 7.

Using the Inclusion-Exclusion Principle:

  • Subtract multiples of 2, 3, 5 and 7.
  • Add multiples of every pair.
  • Subtract multiples of every triplet.
  • Add multiples of all four primes.

The remaining count gives the numbers not divisible by any value from 2 to 10.

C++
#include <iostream>
using namespace std;

int countNonDivisible(int n) {
    
    // Using Inclusion-Exclusion Principle to count numbers divisible by at least one of {2, 3, 5, 7}.
    int res = 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;

    return res;
}

int main() {
    cout << countNonDivisible(11) << endl;
    cout << countNonDivisible(2) << endl;

    return 0;
}
Java
class GFG {
    static int countNonDivisible(int n) {
        
        // Using Inclusion-Exclusion Principle to count numbers divisible by at least one of {2, 3, 5, 7}.
        int res = 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;

        return res;
    }

    public static void main(String[] args) {
        System.out.println(countNonDivisible(11));
        System.out.println(countNonDivisible(2));
    }
}
Python
def countNonDivisible(n):
    
    # Using Inclusion-Exclusion Principle to count numbers divisible by at least one of {2, 3, 5, 7}.
    res = (
        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
    )

    return res

if __name__ == "__main__":
    print(countNonDivisible(11))
    print(countNonDivisible(2))
C#
using System;

class GFG {
    static int countNonDivisible(int n) {
        
        // Using Inclusion-Exclusion Principle to count numbers divisible by at least one of {2, 3, 5, 7}.
        int res = 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;

        return res;
    }

    static void Main() {
        Console.WriteLine(countNonDivisible(11));
        Console.WriteLine(countNonDivisible(2));
    }
}
JavaScript
function countNonDivisible(n) {
    
    // Using Inclusion-Exclusion Principle to count numbers divisible by at least one of {2, 3, 5, 7}.
    let res = n
            - Math.floor(n / 2)
            - Math.floor(n / 3)
            - Math.floor(n / 5)
            - Math.floor(n / 7)
            + Math.floor(n / 6)
            + Math.floor(n / 10)
            + Math.floor(n / 14)
            + Math.floor(n / 15)
            + Math.floor(n / 21)
            + Math.floor(n / 35)
            - Math.floor(n / 30)
            - Math.floor(n / 42)
            - Math.floor(n / 70)
            - Math.floor(n / 105)
            + Math.floor(n / 210);

    return res;
}

// Driver Code
console.log(countNonDivisible(11));
console.log(countNonDivisible(2));

Output
2
1
Comment