Euler’s Totient function for all numbers smaller than or equal to n in java


Following is a program to get the result of Euler’s Totient function for all numbers smaller than or equal to n when n is given.

Program

import java.util.Scanner;

public class EulerTotient {
   public static int gcd(int a,int b){
      int i, hcf = 0;
         for(i = 1; i <= a || i <= b; i++) {
            if( a%i == 0 && b%i == 0 )
            hcf = i;
         }
         return hcf;
   }
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the n value :");
      int n = sc.nextInt();
     
      for (int i = 1; i <= n; i++){
         int x = 1;
         for (int j = 2; j < i; j++){
            if (gcd(j, n) == 1){
               x++;
            }
         }
         System.out.println(x);
      }
   }
}

Output

Enter the n value
10
1
1
1
2
2
2
2
3
3
4

Updated on: 25-Jun-2020

732 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements