From 6351bf2efcb0a07623261c3a3cb4b3de193fcc56 Mon Sep 17 00:00:00 2001 From: Bolot Bekbolotov Date: Sun, 3 Feb 2019 20:48:44 +0600 Subject: [PATCH 1/2] Added Euler's totient Function --- Others/EulersFunction.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Others/EulersFunction.java diff --git a/Others/EulersFunction.java b/Others/EulersFunction.java new file mode 100644 index 000000000000..27f4f1f739f6 --- /dev/null +++ b/Others/EulersFunction.java @@ -0,0 +1,20 @@ +//You can read more about Euler's totient function https://en.wikipedia.org/wiki/Euler%27s_totient_function +public class EulersFunction { + //This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time complexity; + public static int getEuler(int n) { + int result = n; + for (int i = 2; i * i <= n; i++) { + if(n % i == 0) { + while (n % i == 0) n /= i; + result -= result / i; + } + } + if (n > 1) result -= result / n; + return result; + } + public static void main(String[] args) { + for(int i = 1; i < 100; i++) { + System.out.println(getEuler(i)); + } + } +} From 12215a11d3b77c45e074daf096fd1cf43a15cb93 Mon Sep 17 00:00:00 2001 From: Libin Yang Date: Mon, 4 Feb 2019 11:27:42 +0800 Subject: [PATCH 2/2] Update EulersFunction.java --- Others/EulersFunction.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Others/EulersFunction.java b/Others/EulersFunction.java index 27f4f1f739f6..01b7575d4dc5 100644 --- a/Others/EulersFunction.java +++ b/Others/EulersFunction.java @@ -1,6 +1,7 @@ -//You can read more about Euler's totient function https://en.wikipedia.org/wiki/Euler%27s_totient_function +// You can read more about Euler's totient function +// https://en.wikipedia.org/wiki/Euler%27s_totient_function public class EulersFunction { - //This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time complexity; + // This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time complexity; public static int getEuler(int n) { int result = n; for (int i = 2; i * i <= n; i++) { @@ -13,7 +14,7 @@ public static int getEuler(int n) { return result; } public static void main(String[] args) { - for(int i = 1; i < 100; i++) { + for (int i = 1; i < 100; i++) { System.out.println(getEuler(i)); } }