Count integers in a range which are divisible by their euler totient value Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given 2 integers L and R, the task is to find out the number of integers in the range [L, R] such that they are completely divisible by their Euler totient value.Examples: Input: L = 2, R = 3 Output: 1 (2) = 2 => 2 % (2) = 0 (3) = 2 => 3 % (3) = 1 Hence 2 satisfies the condition.Input: L = 12, R = 21 Output: 3 Only 12, 16 and 18 satisfy the condition. Approach: We know that the euler totient function of a number is given as follows: \phi(n) = n * (1 - \frac{1}{p_1}) * (1 - \frac{1}{p_2}) * ... * (1 - \frac{1}{p_k}) Rearranging the terms, we get: \frac{n}{\phi(n)} = \frac{p_1 * p_2 * ... * p_k}{(p_1 - 1) * (p_2 -1) * ... * (p_k -1)} If we take a close look at the RHS, we observe that only 2 and 3 are the primes that satisfy n % = 0. This is because for primes p1 = 2 and p2 = 3, p1 - 1 = 1 and p2 - 1 = 2. Hence, only numbers of the form 2p3q where p >= 1 and q >= 0 need to be counted while lying in the range [L, R].Below is the implementation of the above approach: C++ // C++ implementation of the above approach. #include <bits/stdc++.h> #define ll long long using namespace std; // Function to return a^n ll power(ll a, ll n) { if (n == 0) return 1; ll p = power(a, n / 2); p = p * p; if (n & 1) p = p * a; return p; } // Function to return count of integers // that satisfy n % phi(n) = 0 int countIntegers(ll l, ll r) { ll ans = 0, i = 1; ll v = power(2, i); while (v <= r) { while (v <= r) { if (v >= l) ans++; v = v * 3; } i++; v = power(2, i); } if (l == 1) ans++; return ans; } // Driver Code int main() { ll l = 12, r = 21; cout << countIntegers(l, r); return 0; } Java // Java implementation of the above approach. class GFG { // Function to return a^n static long power(long a, long n) { if (n == 0) return 1; long p = power(a, n / 2); p = p * p; if (n%2== 1) p = p * a; return p; } // Function to return count of integers // that satisfy n % phi(n) = 0 static int countIntegers(long l, long r) { long ans = 0, i = 1; long v = power(2, i); while (v <= r) { while (v <= r) { if (v >= l) ans++; v = v * 3; } i++; v = power(2, i); } if (l == 1) ans++; return (int) ans; } // Driver Code public static void main(String[] args) { long l = 12, r = 21; System.out.println(countIntegers(l, r)); } } // This code contributed by Rajput-Ji Python3 # Python3 implementation of the approach # Function to return a^n def power(a, n): if n == 0: return 1 p = power(a, n // 2) p = p * p if n & 1: p = p * a return p # Function to return count of integers # that satisfy n % phi(n) = 0 def countIntegers(l, r): ans, i = 0, 1 v = power(2, i) while v <= r: while v <= r: if v >= l: ans += 1 v = v * 3 i += 1 v = power(2, i) if l == 1: ans += 1 return ans # Driver Code if __name__ == "__main__": l, r = 12, 21 print(countIntegers(l, r)) # This code is contributed # by Rituraj Jain C# // C# implementation of the above approach. using System; class GFG { // Function to return a^n static long power(long a, long n) { if (n == 0) return 1; long p = power(a, n / 2); p = p * p; if (n % 2 == 1) p = p * a; return p; } // Function to return count of integers // that satisfy n % phi(n) = 0 static int countIntegers(long l, long r) { long ans = 0, i = 1; long v = power(2, i); while (v <= r) { while (v <= r) { if (v >= l) ans++; v = v * 3; } i++; v = power(2, i); } if (l == 1) ans++; return (int) ans; } // Driver Code public static void Main() { long l = 12, r = 21; Console.WriteLine(countIntegers(l, r)); } } /* This code contributed by PrinciRaj1992 */ PHP <?php // PHP implementation of the above approach // Function to return a^n function power($a, $n) { if ($n == 0) return 1; $p = power($a, $n / 2); $p = $p * $p; if ($n & 1) $p = $p * $a; return $p; } // Function to return count of integers // that satisfy n % phi(n) = 0 function countIntegers($l, $r) { $ans = 0 ; $i = 1; $v = power(2, $i); while ($v <= $r) { while ($v <= $r) { if ($v >= $l) $ans++; $v = $v * 3; } $i++; $v = power(2, $i); } if ($l == 1) $ans++; return $ans; } // Driver Code $l = 12; $r = 21; echo countIntegers($l, $r); // This code is contributed by Ryuga ?> JavaScript <script> // JavaScript implementation of the above approach. // Function to return a^n function power(a , n) { if (n == 0) return 1; var p = power(a, parseInt(n / 2)); p = p * p; if (n % 2 == 1) p = p * a; return p; } // Function to return count of integers // that satisfy n % phi(n) = 0 function countIntegers(l , r) { var ans = 0, i = 1; var v = power(2, i); while (v <= r) { while (v <= r) { if (v >= l) ans++; v = v * 3; } i++; v = power(2, i); } if (l == 1) ans++; return parseInt( ans); } // Driver Code var l = 12, r = 21; document.write(countIntegers(l, r)); // This code contributed by Rajput-Ji </script> Output: 3 Comment More infoAdvertise with us Next Article Generate an array having sum of Euler Totient Function of all elements equal to N R rohan23chhabra Follow Improve Article Tags : DSA euler-totient Similar Reads Euler Totient for Competitive Programming What is Euler Totient function(ETF)?Euler Totient Function or Phi-function for 'n', gives the count of integers in range '1' to 'n' that are co-prime to 'n'. It is denoted by \phi(n) .For example the below table shows the ETF value of first 15 positive integers: 3 Important Properties of Euler Totie 8 min read Euler's Totient Function Given an integer n, find the value of Euler's Totient Function, denoted as Φ(n). The function Φ(n) represents the count of positive integers less than or equal to n that are relatively prime to n. Euler's Totient function Φ(n) for an input n is the count of numbers in {1, 2, 3, ..., n-1} that are re 10 min read Count of non co-prime pairs from the range [1, arr[i]] for every array element Given an array arr[] consisting of N integers, the task for every ith element of the array is to find the number of non co-prime pairs from the range [1, arr[i]]. Examples: Input: N = 2, arr[] = {3, 4}Output: 2 4Explanation: All non-co-prime pairs from the range [1, 3] are (2, 2) and (3, 3).All non- 13 min read Generate an array having sum of Euler Totient Function of all elements equal to N Given a positive integer N, the task is to generate an array such that the sum of the Euler Totient Function of each element is equal to N. Examples: Input: N = 6Output: 1 6 2 3 Input: N = 12Output: 1 12 2 6 3 4 Approach: The given problem can be solved based on the divisor sum property of the Euler 5 min read Count all possible values of K less than Y such that GCD(X, Y) = GCD(X+K, Y) Given two integers X and Y, the task is to find the number of integers, K, such that gcd(X, Y) is equal to gcd(X+K, Y), where 0 < K <Y. Examples: Input: X = 3, Y = 15Output: 4Explanation: All possible values of K are {0, 3, 6, 9} for which GCD(X, Y) = GCD(X + K, Y). Input: X = 2, Y = 12Output: 8 min read 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: 3 Explanation: All possible integers which ar 5 min read Find the number of primitive roots modulo prime Given a prime p . The task is to count all the primitive roots of p .A primitive root is an integer x (1 <= x < p) such that none of the integers x - 1, x2 - 1, ...., xp - 2 - 1 are divisible by p but xp - 1 - 1 is divisible by p . Examples: Input: P = 3 Output: 1 The only primitive root modul 5 min read Compute power of power k times % m Given x, k and m. Compute (xxxx...k)%m, x is in power k times. Given x is always prime and m is greater than x. Examples: Input : 2 3 3 Output : 1 Explanation : ((2 ^ 2) ^ 2) % 3 = (4 ^ 2) % 3 = 1 Input : 3 2 3 Output : 0 Explanation : (3^3)%3 = 0 A naive approach is to compute the power of x k time 15+ min read Primitive root of a prime number n modulo n Given a prime number n, the task is to find its primitive root under modulo n. The primitive root of a prime number n is an integer r between[1, n-1] such that the values of r^x(mod n) where x is in the range[0, n-2] are different. Return -1 if n is a non-prime number. Examples: Input : 7 Output : S 15 min read Euler's Totient function for all numbers smaller than or equal to n Euler's Totient function ?(n) for an input n is the count of numbers in {1, 2, 3, ..., n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. For example, ?(4) = 2, ?(3) = 2 and ?(5) = 4. There are 2 numbers smaller or equal to 4 that are relatively pri 13 min read Like