Given three integers N, L, and R. The task is to calculate the number of natural numbers in the range [L, R] (both inclusive) which are relatively prime with N.
Input: N = 10, L = 1, R = 25
10 natural numbers (in the range 1 to 25) are relatively prime to 10.
They are 1, 3, 7, 9, 11, 13, 17, 19, 21, 23.
Input: N = 12, L = 7, R = 38
11 natural numbers (in the range 1 to 38) are relatively prime to 12.
They are 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37.
- At first, factorize the number N. Thus, find out all the prime factors of N.
- Store prime factors of the number N in an array.
- We can determine the total number of natural numbers which are not greater than R and are divisible by prime factors of N.
- Suppose that the value is y. So, exactly y natural numbers not greater than R have at least a single common divisor with N.
- So, these y numbers can not be relatively prime to N.
- Thus, the number of natural number not greater than R which are relatively prime to N will be R – y .
- Now, similarly we need to find out the number of relatively prime numbers of N which are not greater than L-1.
- Then, subtract the result for L-1 from the answer for R.
Below is the implementation of the above approach:
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of Double Prime numbers in a given range L to R
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count numbers from range whose prime factors are only 2 and 3
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Queries for the difference between the count of composite and prime numbers in a given range
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Sum of all natural numbers in range L to R
- Sum of all odd natural numbers in range L and R
- Sum of range in a series of first odd then even natural numbers
- Number of distinct prime factors of first n natural numbers
- Sum of all the prime numbers in a given range
- Prime numbers in a given range using STL | Set 2
- Sum of all prime divisors of all the numbers in range L-R
- Print prime numbers in a given range using C++ STL
- Count of pairs with sum N from first N natural numbers
- K-Primes (Numbers with k prime factors) in a range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : mohit kumar 29