aboutsummaryrefslogtreecommitdiff
path: root/challenge-175/laurent-rosenfeld/java/PerfectTotient.java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-175/laurent-rosenfeld/java/PerfectTotient.java')
-rw-r--r--challenge-175/laurent-rosenfeld/java/PerfectTotient.java37
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");
+ }
+}