# Count of integers up to N which are non divisors and non coprime with N

Given an integer N, the task is to find the count of all possible integers less than N satisfying the following properties:

• The number is not coprime with N i.e their GCD is greater than 1.
• The number is not a divisor of N.

Examples:

Input: N = 10
Output:
Explanation:
All possible integers which are less than 10 and are neither divisors nor coprime with 10, are {4, 6, 8}.
Therefore, the required count is 3.

Input: N = 42
Ouput: 23

Approach:
Follow the steps below to solve the problem:

Total count = N – Euler’s totient(N) – Divisor count(N)

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement ` `// the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count ` `// of integers less than N ` `// satisfying given conditions ` `int` `count(``int` `n) ` `{ ` `    ``// Stores Euler counts ` `    ``int` `phi[n + 1] = { 0 }; ` ` `  `    ``// Store Divisor counts ` `    ``int` `divs[n + 1] = { 0 }; ` ` `  `    ``// Based on Sieve of Eratosthenes ` `    ``for` `(``int` `i = 1; i <= n; i++) { ` ` `  `        ``phi[i] += i; ` ` `  `        ``// Update phi values of all ` `        ``// multiples of i ` `        ``for` `(``int` `j = i * 2; j <= n; j += i) ` `            ``phi[j] -= phi[i]; ` ` `  `        ``// Update count of divisors ` `        ``for` `(``int` `j = i; j <= n; j += i) ` `            ``divs[j]++; ` `    ``} ` ` `  `    ``// Return the final count ` `    ``return` `(n - phi[n] - divs[n] + 1); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``int` `N = 42; ` ` `  `    ``cout << count(N); ` `    ``return` `0; ` `} `

## Java

 `// Java program to implement  ` `// the above approach  ` `import` `java.util.Arrays; ` ` `  `class` `GFG{ ` `     `  `// Function to return the count  ` `// of integers less than N  ` `// satisfying given conditions ` `public` `static` `int` `count(``int` `n)  ` `{  ` `     `  `    ``// Stores Euler counts  ` `    ``int` `[]phi = ``new` `int``[n + ``1``]; ` `    ``Arrays.fill(phi, ``0``); ` ` `  `    ``// Store Divisor counts  ` `    ``int` `[]divs = ``new` `int``[n + ``1``]; ` `    ``Arrays.fill(divs, ``0``); ` `     `  `    ``// Based on Sieve of Eratosthenes  ` `    ``for``(``int` `i = ``1``; i <= n; i++) ` `    ``{  ` `        ``phi[i] += i;  ` ` `  `        ``// Update phi values of all  ` `        ``// multiples of i  ` `        ``for``(``int` `j = i * ``2``; j <= n; j += i)  ` `            ``phi[j] -= phi[i];  ` ` `  `        ``// Update count of divisors  ` `        ``for``(``int` `j = i; j <= n; j += i)  ` `            ``divs[j]++;  ` `    ``}  ` ` `  `    ``// Return the final count  ` `    ``return` `(n - phi[n] - divs[n] + ``1``);  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `main(String []args)  ` `{  ` `    ``int` `N = ``42``;  ` ` `  `    ``System.out.println(count(N));  ` `}  ` `} ` ` `  `// This code is contributed by grand_master `

## Python3

 `# Python3 program to implement ` `# the above approach ` ` `  `# Function to return the count ` `# of integers less than N ` `# satisfying given conditions ` `def` `count(n): ` `     `  `    ``# Stores Euler counts ` `    ``phi ``=` `[``0``] ``*` `(n ``+` `1``) ` `     `  `    ``# Store Divisor counts ` `    ``divs ``=` `[``0``] ``*` `(n ``+` `1``) ` `     `  `    ``# Based on Sieve of Eratosthenes ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `        ``phi[i] ``+``=` `i ` `         `  `        ``# Update phi values of all ` `        ``# multiples of i ` `        ``for` `j ``in` `range``(i ``*` `2``, n ``+` `1``, i): ` `            ``phi[j] ``-``=` `phi[i]; ` ` `  `        ``# Update count of divisors ` `        ``for` `j ``in` `range``(i, n ``+` `1``, i): ` `            ``divs[j] ``+``=` `1` `             `  `    ``# Return the final count ` `    ``return` `(n ``-` `phi[n] ``-` `divs[n] ``+` `1``); ` `     `  `# Driver code  ` `if` `__name__ ``=``=` `'__main__'``:  ` `     `  `    ``N ``=` `42` `     `  `    ``print``(count(N)) ` ` `  `# This code is contributed by jana_sayantan `

## C#

 `// C# program to implement  ` `// the above approach  ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Function to return the count  ` `// of integers less than N  ` `// satisfying given conditions ` `public` `static` `int` `count(``int` `n)  ` `{  ` `     `  `    ``// Stores Euler counts  ` `    ``int` `[]phi = ``new` `int``[n + 1]; ` `     `  `    ``// Store Divisor counts  ` `    ``int` `[]divs = ``new` `int``[n + 1]; ` `     `  `    ``// Based on Sieve of Eratosthenes  ` `    ``for``(``int` `i = 1; i <= n; i++) ` `    ``{  ` `        ``phi[i] += i;  ` ` `  `        ``// Update phi values of all  ` `        ``// multiples of i  ` `        ``for``(``int` `j = i * 2; j <= n; j += i)  ` `            ``phi[j] -= phi[i];  ` ` `  `        ``// Update count of divisors  ` `        ``for``(``int` `j = i; j <= n; j += i)  ` `            ``divs[j]++;  ` `    ``}  ` ` `  `    ``// Return the final count  ` `    ``return` `(n - phi[n] - divs[n] + 1);  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `Main(String []args)  ` `{  ` `    ``int` `N = 42;  ` ` `  `    ``Console.WriteLine(count(N));  ` `}  ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```23
```

Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.