aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-31 17:42:46 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-31 17:42:46 +0100
commit3371c15c1a26dd6f610f23e6489f48f0a12dd00b (patch)
tree28c202135ffe3bdc0acda195463b826882315311
parentad862095a63527e48c12b812a67b94ee112c3c5a (diff)
downloadperlweeklychallenge-club-3371c15c1a26dd6f610f23e6489f48f0a12dd00b.tar.gz
perlweeklychallenge-club-3371c15c1a26dd6f610f23e6489f48f0a12dd00b.tar.bz2
perlweeklychallenge-club-3371c15c1a26dd6f610f23e6489f48f0a12dd00b.zip
- Added more guest contributions by Laurent Rosenfeld.
-rw-r--r--challenge-175/laurent-rosenfeld/dart/ch-2.dart29
-rw-r--r--challenge-175/laurent-rosenfeld/ruby/ch-2.rb24
-rw-r--r--challenge-175/laurent-rosenfeld/scala/ch-2.scala37
-rw-r--r--challenge-175/laurent-rosenfeld/tcl/ch-2.tcl36
4 files changed, 126 insertions, 0 deletions
diff --git a/challenge-175/laurent-rosenfeld/dart/ch-2.dart b/challenge-175/laurent-rosenfeld/dart/ch-2.dart
new file mode 100644
index 0000000000..475955d7f3
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/dart/ch-2.dart
@@ -0,0 +1,29 @@
+import "dart:io";
+
+var cache = List<int>.filled(10000, 0, growable: true);
+
+void main() {
+ cache[0] = 0;
+ var count = 0;
+ var i = 1;
+ while (count < 20) {
+ if (is_perfect_totient(i)) {
+ stdout.write("$i ");
+ count++;
+ }
+ i++;
+ }
+ print(" ");
+}
+
+bool is_perfect_totient(n) {
+ var tot = 0;
+ for (int i = 1; i < n; i++ ) {
+ if (i.gcd(n) == 1) {
+ tot++;
+ }
+ }
+ int sum = tot + cache[tot];
+ cache[n] = sum;
+ return n == sum;
+}
diff --git a/challenge-175/laurent-rosenfeld/ruby/ch-2.rb b/challenge-175/laurent-rosenfeld/ruby/ch-2.rb
new file mode 100644
index 0000000000..879107e1c5
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/ruby/ch-2.rb
@@ -0,0 +1,24 @@
+$cache = Array.new(10000, 0) # global variables require $
+
+def is_perfect_totient(n)
+ tot = 0
+ for i in 1..(n - 1)
+ if n.gcd(i) == 1
+ tot += 1
+ end
+ end
+ sum = tot + $cache[tot]
+ $cache[n] = sum;
+ return sum == n
+end
+
+i = 1
+count = 0
+while count < 20
+ if is_perfect_totient(i)
+ printf("%d ", i)
+ count += 1
+ end
+ i += 1
+end
+print("\n")
diff --git a/challenge-175/laurent-rosenfeld/scala/ch-2.scala b/challenge-175/laurent-rosenfeld/scala/ch-2.scala
new file mode 100644
index 0000000000..bbd7c7da3a
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/scala/ch-2.scala
@@ -0,0 +1,37 @@
+object PerfectTotient extends App {
+
+ var cache = new Array[Int](10000)
+
+ def gcd(a: Int, b: Int): Int = {
+ var (i, j) = (a, b)
+ while (j > 0) {
+ var t = i
+ i = j
+ j = t % j
+ }
+ return i
+ }
+
+ def is_perfect_totient(n: Int): Boolean = {
+ var tot = 0
+ for (i <- 1 to (n - 1)) {
+ if (gcd(n, i) == 1) {
+ tot += 1
+ }
+ }
+ val sum = tot + cache(tot)
+ cache(n) = sum
+ return n == sum
+ }
+
+ var i = 1
+ var count = 0
+ while (count < 20) {
+ if (is_perfect_totient(i)) {
+ count += 1
+ printf("%d ", i)
+ }
+ i += 1
+ }
+ println("")
+}
diff --git a/challenge-175/laurent-rosenfeld/tcl/ch-2.tcl b/challenge-175/laurent-rosenfeld/tcl/ch-2.tcl
new file mode 100644
index 0000000000..92aaa64301
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/tcl/ch-2.tcl
@@ -0,0 +1,36 @@
+array set cache {}
+
+set cache(0) 0
+
+proc gcd {i j} {
+ while {$j != 0} {
+ set t [expr {$i % $j}]
+ set i $j
+ set j $t
+ }
+ return $i
+}
+
+proc is_perfect_totient {n} {
+ global cache
+ set tot 0
+ for {set i 1} {$i < $n} {incr i} {
+ if [ expr [gcd $i $n] == 1 ] {
+ incr tot
+ }
+ }
+ set sum [expr $tot + $cache($tot)]
+ set cache($n) $sum
+ return [ expr $n == $sum ? 1 : 0]
+}
+
+set i 1
+set count 0
+while { $count < 20 } {
+ if [ is_perfect_totient $i ] {
+ puts -nonewline "${i} "
+ incr count
+ }
+ incr i
+}
+puts ""