- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements