Skip to content
Related Articles
Count of numbers up to N having at least one prime factor common with N
• Last Updated : 31 May, 2021

Given an integer N, the task is to count the number of integers from the range [1, N] having at least one in common prime factor with N other than 1.

Examples:

Input: N = 5
Output: 1
Explanation:
Since 5 is prime. Therefore, there is no other number which is at most N, that has at least one common factor with N except N itself. Therefore, the count is 1.

Input: N = 12
Output: 8

Naive Approach: The simplest approach to solve the given problem is to iterate through all the numbers over the range [1, N], and for every element, if the GCD of each number with N is greater than 1, then increment the count. Otherwise, check for the next number. After checking all the numbers, we print the total count obtained.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to count all the numbers``// in the range [1, N] having common``// factor with N other than 1``int` `countNumbers(``int` `N)``{``    ``// Stores the count of numbers``    ``// having more than 1 factor with N``    ``int` `count = 0;` `    ``// Iterate over the range [1, N]``    ``for` `(``int` `i = 1; i <= N; i++) {` `        ``// If gcd is not 1 then``        ``// increment the count``        ``if` `(__gcd(i, N) != 1)``            ``count++;``    ``}` `    ``// Print the resultant count``    ``cout << count;``}` `// Driver Code``int` `main()``{``    ``int` `N = 5;``    ``countNumbers(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to count all the numbers``// in the range [1, N] having common``// factor with N other than 1``static` `void` `countNumbers(``int` `N)``{``    ` `    ``// Stores the count of numbers``    ``// having more than 1 factor with N``    ``int` `count = ``0``;` `    ``// Iterate over the range [1, N]``    ``for``(``int` `i = ``1``; i <= N; i++)``    ``{``        ` `        ``// If gcd is not 1 then``        ``// increment the count``        ``if` `(__gcd(i, N) != ``1``)``            ``count++;``    ``}` `    ``// Print the resultant count``    ``System.out.print(count);``}` `static` `int` `__gcd(``int` `a, ``int` `b)``{``    ``return` `b == ``0` `? a : __gcd(b, a % b);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``5``;``    ``countNumbers(N);``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python 3 program for the above approach``import` `math` `# Function to count all the numbers``# in the range [1, N] having common``# factor with N other than 1``def` `countNumbers(N):` `    ``# Stores the count of numbers``    ``# having more than 1 factor with N``    ``count ``=` `0` `    ``# Iterate over the range [1, N]``    ``for` `i ``in` `range``(``1``, N ``+` `1``):` `        ``# If gcd is not 1 then``        ``# increment the count``        ``if` `(math.gcd(i, N) !``=` `1``):``            ``count ``+``=` `1` `    ``# Print the resultant count``    ``print``(count)`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `5``    ``countNumbers(N)` `    ``# This code is contributed by ukasp.`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG{` `// Function to count all the numbers``// in the range [1, N] having common``// factor with N other than 1``static` `void` `countNumbers(``int` `N)``{``    ` `    ``// Stores the count of numbers``    ``// having more than 1 factor with N``    ``int` `count = 0;` `    ``// Iterate over the range [1, N]``    ``for``(``int` `i = 1; i <= N; i++)``    ``{``        ` `        ``// If gcd is not 1 then``        ``// increment the count``        ``if` `(__gcd(i, N) != 1)``            ``count++;``    ``}` `    ``// Print the resultant count``    ``Console.Write(count);``}` `static` `int` `__gcd(``int` `a, ``int` `b)``{``    ``return` `b == 0 ? a : __gcd(b, a % b);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 5;``    ``countNumbers(N);``}``}` ` `  `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`1`

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

Efficient Approach: The above approach can also be optimized by using Euler’s totient function which gives the count of numbers less than N having no common prime factor with N. Follow the steps below to solve the problem:

• Initialize a variable, say count that stores the numbers having common prime factors other than 1.
• Define a function, phi() to calculate the value of Euler’s totient function which represents the count of integers less than N having no common factor with N.
• After completing the above steps, print the value of (N – phi(N) as the resultant count.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to calculate the value of``// Euler's totient function``int` `phi(``int` `N)``{``    ``// Initialize result with N``    ``int` `result = N;` `    ``// Find all prime factors of N``    ``// and subtract their multiples``    ``for` `(``int` `p = 2; p * p <= N; ++p) {` `        ``// Check if p is a prime factor``        ``if` `(N % p == 0) {` `            ``// If found to be true,``            ``// then update N and result``            ``while` `(N % p == 0)``                ``N /= p;` `            ``result -= result / p;``        ``}``    ``}` `    ``// If N has a prime factor greater``    ``// than sqrt(N), then there can be``    ``// at-most one such prime factor``    ``if` `(N > 1)``        ``result -= result / N;` `    ``return` `result;``}` `// Function to count all the numbers``// in the range [1, N] having common``// factor with N other than 1``int` `countNumbers(``int` `N)``{``    ``// Stores the resultant count``    ``int` `count = N - phi(N);` `    ``// Print the count``    ``cout << count;``}` `// Driver Code``int` `main()``{``    ``int` `N = 5;``    ``countNumbers(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG``{``    ` ` ``// Function to calculate the value of``// Euler's totient function``static` `int` `phi(``int` `N)``{``  ` `    ``// Initialize result with N``    ``int` `result = N;` `    ``// Find all prime factors of N``    ``// and subtract their multiples``    ``for` `(``int` `p = ``2``; p * p <= N; ++p)``    ``{` `        ``// Check if p is a prime factor``        ``if` `(N % p == ``0``) {` `            ``// If found to be true,``            ``// then update N and result``            ``while` `(N % p == ``0``)``                ``N /= p;` `            ``result -= result / p;``        ``}``    ``}` `    ``// If N has a prime factor greater``    ``// than sqrt(N), then there can be``    ``// at-most one such prime factor``    ``if` `(N > ``1``)``        ``result -= result / N;` `    ``return` `result;``}` `// Function to count all the numbers``// in the range [1, N] having common``// factor with N other than 1``static` `void` `countNumbers(``int` `N)``{``  ` `    ``// Stores the resultant count``    ``int` `count = N - phi(N);` `    ``// Print the count``   ``System.out.print(count);``}` `  ``// Driver code``public` `static` `void` `main (String[] args)``{``     ``int` `N = ``5``;``    ``countNumbers(N);``}``}` `// This code is contributed by offbeat.`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG``{``    ` ` ``// Function to calculate the value of``// Euler's totient function``static` `int` `phi(``int` `N)``{``  ` `    ``// Initialize result with N``    ``int` `result = N;` `    ``// Find all prime factors of N``    ``// and subtract their multiples``    ``for` `(``int` `p = 2; p * p <= N; ++p)``    ``{` `        ``// Check if p is a prime factor``        ``if` `(N % p == 0) {` `            ``// If found to be true,``            ``// then update N and result``            ``while` `(N % p == 0)``                ``N /= p;` `            ``result -= result / p;``        ``}``    ``}` `    ``// If N has a prime factor greater``    ``// than sqrt(N), then there can be``    ``// at-most one such prime factor``    ``if` `(N > 1)``        ``result -= result / N;` `    ``return` `result;``}` `// Function to count all the numbers``// in the range [1, N] having common``// factor with N other than 1``static` `void` `countNumbers(``int` `N)``{``  ` `    ``// Stores the resultant count``    ``int` `count = N - phi(N);` `    ``// Print the count``   ``Console.Write(count);``}` `  ``// Driver code``public` `static` `void` `Main(String[] args)``{``     ``int` `N = 5;``    ``countNumbers(N);``}``}` `// This code contributed by shikhasingrajput`

## Javascript

 ``
Output:
`1`

Time Complexity: O(N1/2)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up