From 925d4a68cbfd366160835a465ea3dacc774c6203 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 30 Jul 2022 16:21:35 +0100 Subject: - Added guest solutions by Laurent Rosenfeld. --- .../laurent-rosenfeld/java/PerfectTotient.java | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-175/laurent-rosenfeld/java/PerfectTotient.java (limited to 'challenge-175/laurent-rosenfeld/java/PerfectTotient.java') diff --git a/challenge-175/laurent-rosenfeld/java/PerfectTotient.java b/challenge-175/laurent-rosenfeld/java/PerfectTotient.java new file mode 100644 index 0000000000..7f46773ec8 --- /dev/null +++ b/challenge-175/laurent-rosenfeld/java/PerfectTotient.java @@ -0,0 +1,37 @@ +public class PerfectTotient { + + static int[] cache = new int[10000]; + + public static int gcd(int i, int j) { + while (j != 0) { + int temp = i % j; + i = j; + j = temp; + } + return i; + } + public static boolean isPerfectTotient(int n) { + int tot = 0; + for (int i = 1; i < n; i++) { + if (gcd(n, i) == 1) { + tot++; + } + } + int sum = tot + cache[tot]; + cache[n] = sum; + return n == sum; + } + + public static void main(String[] args) { + int i = 0; + int count = 0; + while (count < 20) { + if (isPerfectTotient(i)) { + System.out.printf("%d ", i); + count++; + } + i++; + } + System.out.printf("%s", "\n"); + } +} -- cgit