Check if Euler Totient Function is same for a given number and twice of that number
Last Updated :
15 Jul, 2025
Given an integer N, the task is to check whether the Euler’s Totient Function of N and 2 * N are the same or not. If they are found to be the same, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 9
Output: Yes
Explanation:
Let phi() be the Euler Totient function
Since 1, 2, 4, 5, 7, 8 have GCD 1 with 9. Therefore, phi(9) = 6.
Since 1, 5, 7, 11, 13, 17 have GCD 1 with 18. Therefore, phi(18) = 6.
Therefore, phi(9) and phi(18) are equal.
Input: N = 14
Output: No
Explanation:
Let phi() be the Euler Totient function, then
Since 1, 3 have GCD 1 with 4. Therefore, phi(4) = 2.
Since 1, 3, 5, 7 have GCD 1 with 8. Therefore, phi(8) = 4.
Therefore, phi(4) and phi(8) are not the same.
Naive Approach: The simplest approach is to find Euler’s Totient Function of N and 2 * N. If they are the same, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the Euler's
// Totient Function
int phi(int n)
{
// Initialize result as N
int result = 1;
// Consider all prime factors
// of n and subtract their
// multiples from result
for (int p = 2; p < n; p++) {
if (__gcd(p, n) == 1) {
result++;
}
}
// Return the count
return result;
}
// Function to check if phi(n)
// is equals phi(2*n)
bool sameEulerTotient(int n)
{
return phi(n) == phi(2 * n);
}
// Driver Code
int main()
{
int N = 13;
if (sameEulerTotient(N))
cout << "Yes";
else
cout << "No";
}
Java
// Java program of
// the above approach
import java.util.*;
class GFG{
// Function to find the Euler's
// Totient Function
static int phi(int n)
{
// Initialize result as N
int result = 1;
// Consider all prime factors
// of n and subtract their
// multiples from result
for (int p = 2; p < n; p++)
{
if (__gcd(p, n) == 1)
{
result++;
}
}
// Return the count
return result;
}
// Function to check if phi(n)
// is equals phi(2*n)
static boolean sameEulerTotient(int n)
{
return phi(n) == phi(2 * n);
}
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 = 13;
if (sameEulerTotient(N))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program of the
# above approach
# Function to find the Euler's
# Totient Function
def phi(n):
# Initialize result as N
result = 1
# Consider all prime factors
# of n and subtract their
# multiples from result
for p in range(2, n):
if (__gcd(p, n) == 1):
result += 1
# Return the count
return result
# Function to check if phi(n)
# is equals phi(2*n)
def sameEulerTotient(n):
return phi(n) == phi(2 * n)
def __gcd(a, b):
return a if b == 0 else __gcd(b, a % b)
# Driver Code
if __name__ == '__main__':
N = 13
if (sameEulerTotient(N)):
print("Yes")
else:
print("No")
# This code is contributed by Amit Katiyar
C#
// C# program of
// the above approach
using System;
class GFG{
// Function to find the Euler's
// Totient Function
static int phi(int n)
{
// Initialize result as N
int result = 1;
// Consider all prime factors
// of n and subtract their
// multiples from result
for (int p = 2; p < n; p++)
{
if (__gcd(p, n) == 1)
{
result++;
}
}
// Return the count
return result;
}
// Function to check if phi(n)
// is equals phi(2*n)
static bool sameEulerTotient(int n)
{
return phi(n) == phi(2 * n);
}
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 = 13;
if (sameEulerTotient(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript program for
//the above approach
// Function to find the Euler's
// Totient Function
function phi(n)
{
// Initialize result as N
let result = 1;
// Consider all prime factors
// of n and subtract their
// multiples from result
for (let p = 2; p < n; p++)
{
if (__gcd(p, n) == 1)
{
result++;
}
}
// Return the count
return result;
}
// Function to check if phi(n)
// is equals phi(2*n)
function sameEulerTotient(n)
{
return phi(n) == phi(2 * n);
}
function __gcd( a, b)
{
return b == 0 ? a :__gcd(b, a % b);
}
// Driver Code
let N = 13;
if (sameEulerTotient(N))
document.write("Yes");
else
document.write("No");
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the key observation is that there is no need to calculate the Euler’s Totient Function as only odd numbers follow the property phi(N) = phi(2*N), where phi() is the Euler's Totient Function.
Therefore, the idea is to check if N is odd or not. If found to be true, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if phi(n)
// is equals phi(2*n)
bool sameEulerTotient(int N)
{
// Return if N is odd
return (N & 1);
}
// Driver Code
int main()
{
int N = 13;
// Function Call
if (sameEulerTotient(N))
cout << "Yes";
else
cout << "No";
}
Java
// Java program of the above approach
class GFG{
// Function to check if phi(n)
// is equals phi(2*n)
static int sameEulerTotient(int N)
{
// Return if N is odd
return (N & 1);
}
// Driver code
public static void main(String[] args)
{
int N = 13;
// Function call
if (sameEulerTotient(N) == 1)
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Dewanti
Python3
# Python3 program of the above approach
# Function to check if phi(n)
# is equals phi(2*n)
def sameEulerTotient(N):
# Return if N is odd
return (N & 1);
# Driver code
if __name__ == '__main__':
N = 13;
# Function call
if (sameEulerTotient(N) == 1):
print("Yes");
else:
print("No");
# This code is contributed by Amit Katiyar
C#
// C# program of
// the above approach
using System;
class GFG{
// Function to check if phi(n)
// is equals phi(2*n)
static int sameEulerTotient(int N)
{
// Return if N is odd
return (N & 1);
}
// Driver code
public static void Main(String[] args)
{
int N = 13;
// Function call
if (sameEulerTotient(N) == 1)
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
//Javascript program of the above approach
// Function to check if phi(n)
// is equals phi(2*n)
function sameEulerTotient(N)
{
// Return if N is odd
return (N & 1);
}
// Driver Code
var N = 13;
// Function Call
if (sameEulerTotient(N))
document.write( "Yes");
else
document.write("No");
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 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
Euclidean algorithms (Basic and Extended) The Euclidean algorithm is a way to find the greatest common divisor of two positive integers. GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.Examples:input: a = 12, b = 20Output: 4Explanatio
9 min read
Stein's Algorithm for finding GCD Stein's algorithm or binary GCD algorithm is an algorithm that computes the greatest common divisor of two non-negative integers. Steinâs algorithm replaces division with arithmetic shifts, comparisons, and subtraction.Examples: Input: a = 17, b = 34 Output : 17Input: a = 50, b = 49Output: 1Algorith
14 min read
Program to find LCM of two numbers Given two positive integers a and b. Find the Least Common Multiple (LCM) of a and b.LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 10, b = 5Output : 10Explanation : 10 is the smallest number divisible by both 10 and 5Input : a = 5, b = 11Output : 55Expla
5 min read
GCD, LCM and Distributive Property Given three integers x, y, z, the task is to compute the value of GCD(LCM(x,y), LCM(x,z)) where, GCD = Greatest Common Divisor, LCM = Least Common MultipleExamples: Input: x = 15, y = 20, z = 100Output: 60Explanation: The GCD of 15 and 20 is 5, and the LCM of 15 and 20 is 60, which is then multiplie
4 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
Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B Given a number n, we need to find the number of ordered pairs of a and b such gcd(a, b) is b itselfExamples : Input : n = 2Output : 3The pairs are (1, 1) (2, 2) and (2, 1) Input : n = 3Output : 5(1, 1) (2, 2) (3, 3) (2, 1) and (3, 1)[Naive Approach] Counting GCD Pairs by Divisor Propertygcd(a, b) =
6 min read
Problems on GCD
Series with largest GCD and sum equals to nGiven integers n and m, construct a strictly increasing sequence of m positive integers with sum exactly equal to n such that the GCD of the sequence is maximized. If multiple sequences have the same maximum GCD, return the lexicographically smallest one. If not possible, return -1.Examples : Input
7 min read
Program to find GCD of floating point numbersThe greatest common divisor (GCD) of two or more numbers, which are not all zero, is the largest positive number that divides each of the numbers. Example: Input : 0.3, 0.9Output : 0.3Explanation: The GCD of 0.3 and 0.9 is 0.3 because both numbers share 0.3 as the largest common divisor.Input : 0.48
4 min read
Largest Subset with GCD 1Given n integers, we need to find size of the largest subset with GCD equal to 1. Input Constraint : n <= 10^5, A[i] <= 10^5Examples: Input : A = {2, 3, 5}Output : 3Explanation: The largest subset with a GCD greater than 1 is {2, 3, 5}, and the GCD of all the elements in the subset is 3.Input
6 min read
Summation of GCD of all the pairs up to nGiven a number n, find sum of all GCDs that can be formed by selecting all the pairs from 1 to n. Examples: Input : n = 4Output : 7Explanation: Numbers from 1 to 4 are: 1, 2, 3, 4Result = gcd(1,2) + gcd(1,3) + gcd(1,4) + gcd(2,3) + gcd(2,4) + gcd(3,4) = 1 + 1 + 1 + 1 + 2 + 1 = 7Input : n = 12Output
10 min read
GCD of more than two (or array) numbersGiven an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
GCD of digits of a given numberGiven a number n, find GCD of its digits. Examples : Input : 345 Output : 1 GCD of 3, 4 and 5 is 1. Input : 2448 Output : 2 GCD of 2, 4, 4 and 8 is 2 We traverse the digits of number one by one using below loop digit = n mod 10; n = n / 10; While traversing digits, we keep track of current GCD and k
4 min read
Maximize GCD of all possible pairs from 1 to NGiven an integer N (> 2), the task is to find the maximum GCD among all pairs possible by the integers in the range [1, N]. Example: Input: N = 5 Output: 2 Explanation : GCD(1, 2) : 1 GCD(1, 3) : 1 GCD(1, 4) : 1 GCD(1, 5) : 1 GCD(2, 3) : 1 GCD(2, 4) : 2 GCD(2, 5) : 1 GCD(3, 4) : 1 GCD(3, 5) : 1 G
3 min read
GCD of two numbers formed by n repeating x and y timesGiven three positive integer n, x, y. The task is to print Greatest Common Divisor of numbers formed by n repeating x times and number formed by n repeating y times. 0 <= n, x, y <= 1000000000.Examples : Input : n = 123, x = 2, y = 3. Output : 123 Number formed are 123123 and 123123123. Greate
6 min read
Program to find Greatest Common Divisor (GCD) of N stringsGiven an array of string arr[], the task is the Greatest Common Divisor of the given array of string. In strings 'A' and 'B', we say "B divides A" if and only if A = concatenation of B more than 1 times.Find the largest string which divides both A and B. Examples: Input: arr[] = { "GFGGFG", "GFGGFG"
13 min read
Find the GCD that lies in given rangeGiven two positive integer a and b and a range [low, high]. The task is to find the greatest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.Examples: Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1,
7 min read
Find the maximum GCD of the siblings of a Binary TreeGiven a 2d-array arr[][] which represents the nodes of a Binary tree, the task is to find the maximum GCD of the siblings of this tree without actually constructing it. Example:Â Â Input: arr[][] = {{4, 5}, {4, 2}, {2, 3}, {2, 1}, {3, 6}, {3, 12}}Â Output: 6Â Explanation:Â Â For the above tree, the maxi
11 min read