Java Coding Test Course, Euler PI

Problem Description

The Euler’s Totient function (φ(n)) represents the number of positive integers that are coprime to n for a given natural number n. For example, φ(1) = 1, φ(2) = 1, φ(3) = 2, φ(4) = 2, φ(5) = 4. The goal of this problem is to write a program that calculates φ(n) for a given natural number n.

Problem: Calculate φ(n)

When a natural number n is given, write a Java program to calculate φ(n) for n. Note that n is an integer between 1 and 10^6 inclusive.

Input Format

  • Natural number n is provided in the first line.

Output Format

  • Output the value of φ(n) for n.

Problem Solving Process

To solve the problem, one must understand the definition and properties of Euler’s Totient function accurately and design an algorithm to calculate it efficiently.

Definition of Euler’s Totient Function

The Euler’s Totient function is calculated as follows:

φ(n) = n * (1 - 1/p1) * (1 - 1/p2) * ... * (1 - 1/pk)
            

Here, p1, p2, …, pk are the distinct prime factors of n. That is, the result can be derived from the number of primes that appear in the prime factorization of n and their product.

Calculating φ(n) through Prime Factorization

The steps to use this equation to calculate φ(n) in Java are as follows:

  1. Set the initial value of n received as input.
  2. Iterate from 2 to the square root of n to find primes.
  3. For each prime p, check if n is divisible by p, and if it is, divide n by p and multiply φ(n) by (1 – 1/p).
  4. Finally, repeat until n becomes 1.
  5. Output the calculated value of φ(n).

Java Code Example

Below is an example of Java code that calculates φ(n):

import java.util.Scanner;

public class EulerTotient {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int originalN = n;
        double result = n;

        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }
                result *= (1.0 - (1.0 / (double)i));
            }
        }

        if (n > 1) {
            result *= (1.0 - (1.0 / (double)n));
        }

        System.out.println((int)result);
    }
}
            

This code calculates and outputs φ(n) for the value of n entered by the user. It has calculated φ(n) in cases where it can be divided by primes and the response is provided accordingly.

Efficiency Analysis

The time complexity of this algorithm is O(√n) because it needs to iterate up to the square root of n to find primes. This method can handle cases where n is as large as 10^6 without issues. Moreover, the space complexity is O(1), minimizing additional memory usage, making it efficient.

Conclusion

The Euler’s Totient function is one of the very important concepts in number theory and algorithm problems. Through the process of solving this problem, one can enhance their understanding of primes, prime factorization, and basic mathematical principles. Remember that this is a useful problem for preparing for coding tests using Java.

If you found this article helpful, please subscribe to the blog and check out more coding test courses.