Euler's Totient for First n Natural Numbers (original) (raw)

Last Updated : 11 Apr, 2026

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.

Example:

**Input: n = 5
**Output:
Totient of 1 is 1
Totient of 2 is 1
Totient of 3 is 2
Totient of 4 is 2
Totient of 5 is 4
**Explanation:
1 has 1 coprime number → φ(1) = 1
2 has 1 coprime number (1) → φ(2) = 1
3 has 2 coprime numbers (1,2) → φ(3) = 2
4 has 2 coprime numbers (1,3) → φ(4) = 2
5 has 4 coprime numbers (1,2,3,4) → φ(5) = 4

**Input: n = 12
**Output:
Totient of 1 is 1
Totient of 2 is 1
Totient of 3 is 2
Totient of 4 is 2
Totient of 5 is 4
Totient of 6 is 2
Totient of 7 is 6
Totient of 8 is 4
Totient of 9 is 6
Totient of 10 is 4
Totient of 11 is 10
Totient of 12 is 4
**Explanation:
1 has 1 coprime number → φ(1) = 1 → {1}
2 has 1 coprime number → φ(2) = 1 → {1}
3 has 2 coprime numbers → φ(3) = 2 → {1,2}
4 has 2 coprime numbers → φ(4) = 2 → {1,3}
5 has 4 coprime numbers → φ(5) = 4 → {1,2,3,4}
6 has 2 coprime numbers → φ(6) = 2 → {1,5}
7 has 6 coprime numbers → φ(7) = 6 → {1,2,3,4,5,6}
8 has 4 coprime numbers → φ(8) = 4 → {1,3,5,7}
9 has 6 coprime numbers → φ(9) = 6 → {1,2,4,5,7,8}
10 has 4 coprime numbers → φ(10) = 4 → {1,3,7,9}
11 has 10 coprime numbers → φ(11) = 10 → {1,2,3,4,5,6,7,8,9,10}
12 has 4 coprime numbers → φ(12) = 4 → {1,5,7,11}

Table of Content

[Naive Approach] Iterative GCD Method - O(√n) time and O(1) space

_A simple solution is to iterate through all numbers from 1 to n-1 and count numbers with gcd with n as 1. Below is the implementation of the simple method to compute Euler's Totient function for an input integer n.

C++ `

#include using namespace std;

// Function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }

// A simple method to evaluate Euler Totient Function int etf(int n) { int res = 1; for (int i = 2; i < n; i++) if (gcd(i, n) == 1) res++; return res; }

// Driver Code int main() { int n = 12; cout << etf(n); return 0; }

Java

class GfG {

// Function to return gcd of a and b
static int gcd(int a, int b) {
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

// Function to compute Euler's Totient Function
static int etf(int n) {
    int res = 1;
    for (int i = 2; i < n; i++) {
        if (gcd(i, n) == 1)
            res++;
    }
    return res;
}

// Driver Code public static void main(String[] args) { int n = 12; System.out.println(etf(n)); } }

Python

Function to return gcd of a and b

def gcd(a, b): if a == 0: return b return gcd(b % a, a)

A simple method to evaluate Euler Totient Function

def etf(n): res = 1 for i in range(2, n): if gcd(i, n) == 1: res += 1 return res

Driver Code

if name == "main": n = 12 print(etf(n))

C#

using System;

class GfG {

// Function to return gcd of a and b
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

// A simple method to evaluate Euler Totient Function
static int etf(int n)
{
    int res = 1;
    for (int i = 2; i < n; i++) {
        if (gcd(i, n) == 1)
            res++;
    }
    return res;
}

// Driver Code
static void Main()
{
    int n = 12;
    Console.WriteLine(etf(n));
}

}

JavaScript

// Function to return gcd of a and b function gcd(a, b) { if (a === 0) return b; return gcd(b % a, a); }

// A simple method to evaluate Euler Totient Function function etf(n) { let res = 1; for (let i = 2; i < n; i++) { if (gcd(i, n) === 1) res++; } return res; }

// Driver Code let n = 12; console.log(etf(n));

`

[Efficient Approach] Sieve of Eratosthenes - O(n log(log n)) time and O(n) space

An **Efficient Solution is to use an idea similar to the Sieve of Eratosthenes to precompute all values. The method is based on below product formula.

\phi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)

Below is the complete algorithm:

Step 1. Create an array phi[1..n] to store φ values of all numbers from 1 to n.

Step 2. Initialize all values such that phi[i] stores i. This initialization serves two purposes.

Step 3. Run a loop for p = 2 to n

Step 4. Run a loop from i = 1 to n and print all Phi[i] values.

C++ `

using namespace std;

vector etf(int n) { // Computes totient of all numbers from 1 to n vector phi(n + 1); for (int i = 1; i <= n; i++) phi[i] = i;

// Compute other Phi values
for (int p = 2; p <= n; p++)
{
    // If p is prime
    if (phi[p] == p)
    {
        phi[p] = p - 1;
        // Update multiples of p
        for (int i = 2 * p; i <= n; i += p)
        {
            phi[i] = (phi[i] / p) * (p - 1);
        }
    }
}
return phi;

}

// Driver code int main() { int n = 12; vector phi = etf(n);

for (int i = 1; i <= n; i++)
    cout << "Totient of " << i << " is " << phi[i] << endl;

return 0;

}

Java

class GFG {

static int[] etf(int n)
{
    // Computes totient of all numbers from 1 to n
    int phi[] = new int[n + 1];
    for (int i = 1; i <= n; i++)
        phi[i] = i;

    // Compute other Phi values
    for (int p = 2; p <= n; p++) {

        // If p is prime
        if (phi[p] == p) {
            phi[p] = p - 1;

            // Update multiples of p
            for (int i = 2 * p; i <= n; i += p) {
                phi[i] = (phi[i] / p) * (p - 1);
            }
        }
    }
    return phi;
}

// Driver code
public static void main(String[] args)
{
    int n = 12;
    int[] phi = etf(n);

    for (int i = 1; i <= n; i++)
        System.out.println("Totient of " + i + " is "
                           + phi[i]);
}

}

Python

def etf(n): # Computes totient of all numbers from 1 to n phi = [0] * (n + 1)

for i in range(1, n + 1):
    phi[i] = i

# Compute other Phi values
for p in range(2, n + 1):
    # If p is prime
    if phi[p] == p:
        phi[p] = p - 1

        for i in range(2 * p, n + 1, p):
            phi[i] = (phi[i] // p) * (p - 1)

return phi

Driver code

n = 12 phi = etf(n)

for i in range(1, n + 1): print("Totient of", i, "is", phi[i])

C#

using System;

class GFG { // Computes totient of all numbers from 1 to n static int[] etf(int n) { int[] phi = new int[n + 1];

    for (int i = 1; i <= n; i++)
        phi[i] = i;

    // Compute other Phi values
    for (int p = 2; p <= n; p++)
    {
        // If p is prime
        if (phi[p] == p)
        {
            phi[p] = p - 1;

            // Update multiples of p
            for (int i = 2 * p; i <= n; i += p)
            {
                phi[i] = (phi[i] / p) * (p - 1);
            }
        }
    }

    return phi;
}

// Driver code
public static void Main()
{
    int n = 12;
    int[] phi = etf(n);

    for (int i = 1; i <= n; i++)
        Console.WriteLine("Totient of " + i + " is " + phi[i]);
}

}

JavaScript

function etf(n) { // Computes totient of all numbers from 1 to n let phi = new Array(n + 1); for (let i = 1; i <= n; i++)

    phi[i] = i;

// Compute other Phi values
for (let p = 2; p <= n; p++) {

    // If p is prime
    if (phi[p] == p) {

        phi[p] = p - 1;

        // Update multiples of p
        for (let i = 2 * p; i <= n; i += p) {

            phi[i] = parseInt(phi[i] / p, 10) * (p - 1);
        }
    }
}

return phi;

}

// Driver code let n = 12; let phi = etf(n);

for (let i = 1; i <= n; i++) console.log("Totient of " + i + " is " + phi[i]);

PHP

i<=i <= i<=n; $i++) phi[phi[phi[i] = $i; // Compute other Phi values for($p = 2; p<=p <= p<=n; $p++) { // If p is prime if ($phi[$p] == $p) { phi[phi[phi[p] = $p - 1; // Update multiples of p for($i = 2 * p;p; p;i <= n;n; n;i += $p) { phi[phi[phi[i] = ($phi[$i] / p)∗(p) * (p)(p - 1); } } } return $phi; } // Driver Code $n = 12; phi=etf(phi = etf(phi=etf(n); for($i = 1; i<=i <= i<=n; $i++) echo "Totient of ", i,"is",i, " is ", i,"is",phi[$i], "\n"; ?>

`

Output

Totient of 1 is 1 Totient of 2 is 1 Totient of 3 is 2 Totient of 4 is 2 Totient of 5 is 4 Totient of 6 is 2 Totient of 7 is 6 Totient of 8 is 4 Totient of 9 is 6 Totient of 10 is 4 Totient of 11 is 10...

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

Prime Factorization - O(sqrt(n)*log(n)) time and O(1) space

The sieve-based solution is useful when we have a **large number of queries for computing the totient function.

For numbers upto n, we can use **Prime Factorization.

\phi(n) = \prod_{i=1}^{k} p_i^{\alpha_i - 1} (p_i - 1)

This approach computes φ(n) using prime factorization instead of iterating over all numbers.

For example, φ(12) = { (2^(2-1)) x (2-1) } x { (3^(1-1)) x (3-1) } =4

Note that φ(n) = n - 1 if n is prime.

C++ `

using namespace std;

int etf(int n) { int res = 1; for (int i = 2; i * i <= n; i++) { int c = 0; if (n % i == 0) { while (n % i == 0) { c++; n /= i; } } if (c > 0) { int power = (int)pow(i, c - 1); int sm = (int)pow(i, c - 1) * (i - 1); res *= sm; } } if (n > 1) { res *= (n - 1); } return res; }

// driver code int main() { for (int i = 1; i < 13; i++) { cout << "Totient of " << i << " is: "; cout << etf(i) << endl; } }

Java

class GFG {

static int etf(int n)
{
    int res = 1;
    for (int i = 2; i * i <= n; i++) {
        int c = 0;
        if (n % i == 0) {
            while (n % i == 0) {
                c++;
                n /= i;
            }
        }
        if (c > 0) {
            int power = (int)Math.pow(i, c - 1);
            int sm = (int)Math.pow(i, c - 1) * (i - 1);
            res *= sm;
        }
    }
    if (n > 1) {
        res *= (n - 1);
    }
    return res;
}

// Driver code
public static void main(String[] args)
{
    for (int i = 1; i < 13; i++) {
        System.out.print("Totient of " + i
                         + " is ");
        System.out.println(etf(i));
    }
}

}

Python

import math

def etf(n): res = 1 for i in range(2, n + 1): c = 0 if n % i == 0: while (n % i == 0): c += 1 n //= i if (c > 0): power = math.pow(i, c - 1) m = math.pow(i, c - 1) * (i - 1) res *= m if (n > 1): res *= (n - 1) return int(res)

Driver code

for i in range(1, 13): print("Totient of ", i, " is ", end="") print(etf(i))

C#

// C# program for the above approach using System;

class GFG {

static int etf(int n)
{
    int res = 1;
    for (int i = 2; i * i <= n; i++) {
        int c = 0;
        if (n % i == 0) {
            while (n % i == 0) {
                c++;
                n /= i;
            }
        }
        if (c > 0) {
            int sm
                = (int)Math.Pow(i, c - 1) * (i - 1);
            res *= sm;
        }
    }
    if (n > 1) {
        res *= (n - 1);
    }
    return res;
}

// Driver code
public static void Main()
{
    for (int i = 1; i < 13; i++) {
        Console.Write("Totient of " + i
                      + " is ");
        Console.WriteLine(etf(i));
    }
}

}

JavaScript

function etf(n) { let res = 1; for (let i = 2; i * i <= n; i++) { let c = 0; if (n % i == 0) { while (n % i == 0) { c++; n = parseInt(n / i); } } if (c > 0) { let power = Math.pow(i, c - 1); let sm = Math.pow(i, c - 1) * (i - 1); res *= sm; } } if (n > 1) { res *= (n - 1); } return res; }

// driver code for (let i = 1; i < 13; i++) { console.log("Totient of " + i + " is ", etf(i)); }

`

Output

Totient of 1 is: 1 Totient of 2 is: 1 Totient of 3 is: 2 Totient of 4 is: 2 Totient of 5 is: 4 Totient of 6 is: 2 Totient of 7 is: 6 Totient of 8 is: 4 Totient of 9 is: 6 Totient of 10 is: 4 Totient o...