diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-30 16:21:35 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-30 16:21:35 +0100 |
| commit | 925d4a68cbfd366160835a465ea3dacc774c6203 (patch) | |
| tree | 35449a9b65d9e1653d690c7a8d2de16831b3f915 /challenge-175/laurent-rosenfeld/java/PerfectTotient.java | |
| parent | 8d734718fe8e29e2c7b084c9efa08b1a7894aabe (diff) | |
| download | perlweeklychallenge-club-925d4a68cbfd366160835a465ea3dacc774c6203.tar.gz perlweeklychallenge-club-925d4a68cbfd366160835a465ea3dacc774c6203.tar.bz2 perlweeklychallenge-club-925d4a68cbfd366160835a465ea3dacc774c6203.zip | |
- Added guest solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-175/laurent-rosenfeld/java/PerfectTotient.java')
| -rw-r--r-- | challenge-175/laurent-rosenfeld/java/PerfectTotient.java | 37 |
1 files changed, 37 insertions, 0 deletions
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"); + } +} |
