aboutsummaryrefslogtreecommitdiff
path: root/challenge-175/mohammad-anwar
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-07-30 01:07:04 +0100
committerdrbaggy <js5@sanger.ac.uk>2022-07-30 01:07:04 +0100
commit40550e1e6ea41dabe7a6a72e17f5f56de85fabcd (patch)
treee170a85111547d5b4e09224fe56ecf6788726a6f /challenge-175/mohammad-anwar
parentd69c546a4fda62fcd899662f52a45e3ba03365c3 (diff)
parentc549a7288ca66b1b48e8518454806b538fd97568 (diff)
downloadperlweeklychallenge-club-40550e1e6ea41dabe7a6a72e17f5f56de85fabcd.tar.gz
perlweeklychallenge-club-40550e1e6ea41dabe7a6a72e17f5f56de85fabcd.tar.bz2
perlweeklychallenge-club-40550e1e6ea41dabe7a6a72e17f5f56de85fabcd.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-175/mohammad-anwar')
-rw-r--r--challenge-175/mohammad-anwar/java/theweeklychallenge/FirstPerfectTotient.java81
-rw-r--r--challenge-175/mohammad-anwar/perl/ch-2.pl68
-rw-r--r--challenge-175/mohammad-anwar/python/ch-2.py63
-rw-r--r--challenge-175/mohammad-anwar/raku/ch-2.raku61
-rw-r--r--challenge-175/mohammad-anwar/swift/ch-2.swift91
5 files changed, 364 insertions, 0 deletions
diff --git a/challenge-175/mohammad-anwar/java/theweeklychallenge/FirstPerfectTotient.java b/challenge-175/mohammad-anwar/java/theweeklychallenge/FirstPerfectTotient.java
new file mode 100644
index 0000000000..2c8027e28c
--- /dev/null
+++ b/challenge-175/mohammad-anwar/java/theweeklychallenge/FirstPerfectTotient.java
@@ -0,0 +1,81 @@
+package theweeklychallenge;
+
+/*
+
+Week 175:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-175
+
+Task #2: Perfect Totient Numbers
+
+ Write a script to generate first 20 Perfect Totient Numbers.
+
+Compile and Run:
+
+ mohammad-anwar/java$ javac theweeklychallenge/FirstPerfectTotient.java
+ mohammad-anwar/java$ java theweeklychallenge.FirstPerfectTotient
+
+*/
+
+import java.util.Arrays;
+import java.util.ArrayList;
+import junit.framework.TestCase;
+import static junit.framework.Assert.*;
+
+public class FirstPerfectTotient extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(
+ theweeklychallenge.FirstPerfectTotient.class);
+ }
+
+ public void testFirstPerfectTotient() {
+ Integer[] got = firstPerfectTotient(20);
+ Integer[] exp = {
+ 3, 9, 15, 27, 39, 81, 111, 183, 243,
+ 255, 327, 363, 471, 729, 2187, 2199,
+ 3063, 4359, 4375, 5571
+ };
+
+ assertEquals(Arrays.toString(exp), Arrays.toString(got));
+ }
+
+ public static int gcd(int m, int n) {
+ return n == 0 ? m : gcd(n, m % n);
+ }
+
+ public static boolean isCoprime(int m, int n) {
+ return gcd(m, n) == 1;
+ }
+
+ public static boolean isPerfectTotient(int n) {
+ int i = n;
+ int s = 0;
+ while (i >= 1) {
+ ArrayList<Integer> coprimes = new ArrayList<Integer>();
+ for (int j = 1; j < i; j++) {
+ if (isCoprime(j, i)) {
+ coprimes.add(j);
+ }
+ }
+ i = coprimes.size();
+ s = s + i;
+ }
+
+ return n == s;
+ }
+
+ public static Integer[] firstPerfectTotient(int n) {
+ ArrayList<Integer> fpt = new ArrayList<Integer>();
+
+ int i = 1;
+ while (fpt.size() < n) {
+ if (isPerfectTotient(i)) {
+ fpt.add(i);
+ }
+ i++;
+ }
+
+ return fpt.toArray(new Integer[fpt.size()]);
+ }
+}
diff --git a/challenge-175/mohammad-anwar/perl/ch-2.pl b/challenge-175/mohammad-anwar/perl/ch-2.pl
new file mode 100644
index 0000000000..47d464ad34
--- /dev/null
+++ b/challenge-175/mohammad-anwar/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+=head1
+
+Week 175:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-175
+
+Task #2: Perfect Totient Numbers
+
+ Write a script to generate first 20 Perfect Totient Numbers.
+
+=cut
+
+use v5.36;
+use Test2::V0;
+
+is [
+ 3, 9, 15, 27, 39, 81, 111, 183, 243,
+ 255, 327, 363, 471, 729, 2187, 2199,
+ 3063, 4359, 4375, 5571
+ ], first_perfect_totient(20);
+
+done_testing;
+
+#
+#
+# METHODS
+
+sub is_coprime($a, $b) {
+ if ($a > $b) {
+ ($a, $b) = ($b, $a);
+ }
+ while ($a) {
+ ($a, $b) = ($b % $a, $a);
+ }
+ return $b == 1;
+}
+
+#
+# Simply coded as shown in the example in the wiki page:
+# https://en.wikipedia.org/wiki/Perfect_totient_number
+
+sub is_perfect_totient($n) {
+ my $i = $n;
+ my $s = 0;
+ while ($i >= 1) {
+ my @coprimes = ();
+ foreach (1 .. $i-1) {
+ push @coprimes, $_ if is_coprime $_, $i;
+ }
+ $i = @coprimes;
+ $s = $s + $i;
+ }
+
+ return $n == $s;
+}
+
+sub first_perfect_totient($n) {
+ my @pt = ();
+ my $i = 1;
+ while (@pt < $n) {
+ push @pt, $i if is_perfect_totient $i;
+ $i++;
+ }
+
+ return \@pt;
+}
diff --git a/challenge-175/mohammad-anwar/python/ch-2.py b/challenge-175/mohammad-anwar/python/ch-2.py
new file mode 100644
index 0000000000..70ff32d7e2
--- /dev/null
+++ b/challenge-175/mohammad-anwar/python/ch-2.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python3
+
+'''
+
+Week 175:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-175
+
+Task #2: Perfect Totient Numbers
+
+ Write a script to generate first 20 Perfect Totient Numbers.
+
+'''
+
+import math
+import unittest
+
+def isCoprime(a, b) -> bool:
+ return math.gcd(a, b) == 1
+
+#
+# Simply coded as shown in the example in the wiki page:
+# https://en.wikipedia.org/wiki/Perfect_totient_number
+
+def isPerfectTotient(n) -> bool:
+ i = n
+ s = 0
+ while i >= 1:
+ coprimes = []
+ for j in range(1, i):
+ if isCoprime(i, j):
+ coprimes.append(j)
+
+ i = len(coprimes)
+ s = s + i
+
+ return n == s
+
+def firstPerfectTotient(n):
+ pt = []
+ i = 1
+ while (len(pt) < n):
+ if isPerfectTotient(i):
+ pt.append(i)
+ i = i + 1
+
+ return pt
+
+#
+#
+# Unit test class
+
+class TestPerfectTotient(unittest.TestCase):
+ def test_firstPerfectTotient(self):
+ exp = [
+ 3, 9, 15, 27, 39, 81, 111, 183, 243,
+ 255, 327, 363, 471, 729, 2187, 2199,
+ 3063, 4359, 4375, 5571
+ ]
+ got = firstPerfectTotient(20)
+ self.assertEqual(exp, got)
+
+unittest.main()
diff --git a/challenge-175/mohammad-anwar/raku/ch-2.raku b/challenge-175/mohammad-anwar/raku/ch-2.raku
new file mode 100644
index 0000000000..58917a5d10
--- /dev/null
+++ b/challenge-175/mohammad-anwar/raku/ch-2.raku
@@ -0,0 +1,61 @@
+#!/usr/bin/env raku
+
+=begin pod
+
+Week 175:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-174
+
+Task #2: Perfect Totient Numbers
+
+ Write a script to generate first 20 Perfect Totient Numbers.
+
+=end pod
+
+use Test;
+
+is [
+ 3, 9, 15, 27, 39, 81, 111, 183, 243,
+ 255, 327, 363, 471, 729, 2187, 2199,
+ 3063, 4359, 4375, 5571
+ ], first-perfect-totient(20);
+
+done-testing;
+
+#
+#
+# METHODS
+
+sub is-coprime(Int $a, Int $b --> Bool) {
+ return ($a gcd $b) == 1;
+}
+
+#
+# Simply coded as shown in the example in the wiki page:
+# https://en.wikipedia.org/wiki/Perfect_totient_number
+
+sub is-perfect-totient(Int $n --> Bool) {
+ my Int $i = $n;
+ my Int $s = 0;
+ while $i >= 1 {
+ my Int @coprimes = ();
+ for 1..^$i {
+ @coprimes.push: $_ if is-coprime $_, $i;
+ }
+ $i = @coprimes.elems;
+ $s = $s + $i;
+ }
+
+ return $n == $s;
+}
+
+sub first-perfect-totient(Int $n --> Array[Int]) {
+ my Int @pt = ();
+ my Int $i = 1;
+ while @pt.elems < $n {
+ @pt.push: $i if is-perfect-totient $i;
+ $i++;
+ }
+
+ return @pt;
+}
diff --git a/challenge-175/mohammad-anwar/swift/ch-2.swift b/challenge-175/mohammad-anwar/swift/ch-2.swift
new file mode 100644
index 0000000000..32169f28b0
--- /dev/null
+++ b/challenge-175/mohammad-anwar/swift/ch-2.swift
@@ -0,0 +1,91 @@
+import Foundation
+
+/*
+
+Week 175:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-175
+
+Task #2: Perfect Totient Numbers
+
+ Write a script to generate first 20 Perfect Totient Numbers.
+
+ACTION:
+
+ $ swift ch-2.swift 20
+
+*/
+
+enum ParamError: Error {
+ case missingNumber
+ case invalidNumber
+}
+
+do {
+ let paramCount:Int = Int(CommandLine.argc)
+
+ if paramCount <= 1 {
+ throw ParamError.missingNumber
+ }
+
+ let num:Int = Int(CommandLine.arguments[1])!
+
+ if num >= 1 {
+ var i:Int = 0
+ var j:Int = 1
+ while i < num {
+ if isPerfectTotient(j) {
+ print(j)
+ i = i + 1
+ }
+ j = j + 1
+ }
+ }
+ else {
+ throw ParamError.invalidNumber
+ }
+}
+catch ParamError.missingNumber {
+ print("Missing number.")
+}
+catch ParamError.invalidNumber {
+ print("Invalid number.")
+}
+catch let error {
+ print(error)
+}
+
+//
+//
+// Functions
+
+func isCoprime(_ m: Int, _ n: Int) -> Bool {
+ var a: Int = 0
+ var b: Int = max(m, n)
+ var r: Int = min(m, n)
+
+ while r != 0 {
+ a = b
+ b = r
+ r = a % b
+ }
+
+ return b == 1
+}
+
+func isPerfectTotient(_ n:Int) -> Bool {
+ var i: Int = n
+ var s: Int = 0
+ while i >= 1 {
+ var coprimes:[Int] = []
+ for j in 1..<i {
+ if isCoprime(j, i) {
+ coprimes.append(j)
+ }
+ }
+ i = coprimes.count
+ s = s + i
+ }
+
+ return n == s
+}