Coding tests are a very important part of modern software engineering. Many companies include coding problems in technical interviews to assess applicants’ algorithmic and problem-solving skills. In this course, we will learn about Euler’s Totient Function, a mathematical concept.
Problem Description
The Euler’s Totient Function returns the number of positive integers m for a given integer n. m is an integer that is greater than or equal to 1 and less than or equal to n, and m and n are coprime integers. The goal is to determine the count of m that satisfies gcd(m, n) = 1.
Problem: Compute the Euler’s Totient Function for a given integer n.
Input Format
- 1 ≤ n ≤ 106
Output Format
- The Euler’s Totient value of n
Example
Input:
6
Output:
2
Theoretical Background
The Euler’s Totient Function is defined as follows:
- If n is a prime number: ϕ(n) = n – 1
- If n is not a prime number, using the prime factors p of n: ϕ(n) = n × (1 – 1/p1) × (1 – 1/p2) × … × (1 – 1/pk)
This allows us to find the prime factors of n and calculate ϕ(n) accordingly.
C# Implementation
Now, let’s implement the Euler’s Totient Function using C#. The time complexity of this algorithm is O(√n), which is efficient for finding the prime factors of n.
using System;
class Program
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
Console.WriteLine(EulerPhi(n));
}
static int EulerPhi(int n)
{
int result = n; // Set initial value to n
for (int p = 2; p * p <= n; p++)
{
// Check if p is a prime factor of n
if (n % p == 0)
{
// Remove prime factor by dividing n
while (n % p == 0)
{
n /= p;
}
result -= result / p; // Apply Euler's Totient formula
}
}
// If the remaining n is a prime number
if (n > 1)
{
result -= result / n;
}
return result; // Return the final result
}
}
Code Explanation
The above code is for calculating the Euler’s Totient Function and consists of the following steps:
- Read Input: Accept an integer n from the user.
- Initialize Value: Initialize the result value to the input n.
- Find Prime Factors: Iterate from 2 to the square root of n, checking if n is divisible by p.
- Remove Prime Factors: Divide n by the prime factor p to remove it from n.
- Apply Euler’s Totient Formula: Update the result value using the ϕ(n) formula for the current prime factor p.
- Check Remaining Prime: If the remaining n value is greater than 1, exclude n from the result since it is prime.
- Return Result: Return the final result value.
Performance Optimization
This algorithm operates very quickly within the specified range. However, for faster calculations on various inputs, we can precompute primes. Using the Sieve of Eratosthenes, we can find primes and use them to compute Euler’s Totient values.
By using an array to store primes, we can further improve the function. This method increases speed through comparisons with other numbers and optimizes memory usage.
Conclusion
The Euler’s Totient Function can be easily implemented in managed languages like C#, and by combining theory with code, we can solve algorithm problems more efficiently. In this course, you have learned how to compute the Euler’s Totient Function using various techniques, including basic functions in C# and the Euclidean algorithm for finding the greatest common divisor.
We hope you continue to practice regularly to solve more algorithm problems in future coding tests!