aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-30 16:21:35 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-30 16:21:35 +0100
commit925d4a68cbfd366160835a465ea3dacc774c6203 (patch)
tree35449a9b65d9e1653d690c7a8d2de16831b3f915
parent8d734718fe8e29e2c7b084c9efa08b1a7894aabe (diff)
downloadperlweeklychallenge-club-925d4a68cbfd366160835a465ea3dacc774c6203.tar.gz
perlweeklychallenge-club-925d4a68cbfd366160835a465ea3dacc774c6203.tar.bz2
perlweeklychallenge-club-925d4a68cbfd366160835a465ea3dacc774c6203.zip
- Added guest solutions by Laurent Rosenfeld.
-rw-r--r--challenge-175/laurent-rosenfeld/awk/ch-2.awk31
-rw-r--r--challenge-175/laurent-rosenfeld/bc/ch-2.bc33
-rw-r--r--challenge-175/laurent-rosenfeld/c/ch-2.c38
-rw-r--r--challenge-175/laurent-rosenfeld/d/ch-2.d29
-rw-r--r--challenge-175/laurent-rosenfeld/go/ch-2.go37
-rw-r--r--challenge-175/laurent-rosenfeld/java/PerfectTotient.java37
-rw-r--r--challenge-175/laurent-rosenfeld/javascript/ch-2.js35
-rw-r--r--challenge-175/laurent-rosenfeld/julia/ch-1.jl12
-rw-r--r--challenge-175/laurent-rosenfeld/julia/ch-2.jl19
-rw-r--r--challenge-175/laurent-rosenfeld/kotlin/ch-2.kt37
-rw-r--r--challenge-175/laurent-rosenfeld/nim/ch-2.nim21
-rw-r--r--challenge-175/laurent-rosenfeld/python/ch-1.py23
-rw-r--r--challenge-175/laurent-rosenfeld/python/ch-2.py13
-rw-r--r--challenge-175/laurent-rosenfeld/ring/ch-2.ring33
-rw-r--r--challenge-175/laurent-rosenfeld/ruby/ch-1.rb9
-rw-r--r--challenge-175/laurent-rosenfeld/rust/ch-2.rs30
16 files changed, 437 insertions, 0 deletions
diff --git a/challenge-175/laurent-rosenfeld/awk/ch-2.awk b/challenge-175/laurent-rosenfeld/awk/ch-2.awk
new file mode 100644
index 0000000000..959d5adeba
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/awk/ch-2.awk
@@ -0,0 +1,31 @@
+function gcd (i, j) {
+ while(j != 0) {
+ k = j
+ j = i % j
+ i = k
+ }
+ return i
+}
+function is_perfect_totient (num) {
+ tot = 0
+ for (i = 1; i < num; i++) {
+ if (gcd(num, i) == 1) {
+ tot += 1
+ }
+ }
+ sum = tot + cache[tot]
+ cache[num] = sum
+ return num == sum
+}
+BEGIN {
+ j = 1
+ count = 0
+ while (count < 20) {
+ if (is_perfect_totient(j)) {
+ printf "%d ", j
+ count += 1
+ }
+ j += 1
+ }
+ print " "
+}
diff --git a/challenge-175/laurent-rosenfeld/bc/ch-2.bc b/challenge-175/laurent-rosenfeld/bc/ch-2.bc
new file mode 100644
index 0000000000..d7bb3b48ea
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/bc/ch-2.bc
@@ -0,0 +1,33 @@
+define gcd (i, j) {
+ while(j != 0) {
+ k = j
+ j = i % j
+ i = k
+ }
+ return i
+}
+
+define is_perfect_totient (num) {
+ tot = 0
+ for (i = 1; i < num; i++) {
+ if (gcd(num, i) == 1) {
+ tot += 1
+ }
+ }
+ sum = tot + cache[tot]
+ cache[num] = sum
+ return num == sum
+}
+
+j = 1
+count = 0
+# we only go to 15 (not 20) because bc is very slow
+while (count <= 15) {
+ if (is_perfect_totient(j)) {
+ print j, " "
+ count += 1
+ }
+ j += 1
+}
+print "\n"
+quit
diff --git a/challenge-175/laurent-rosenfeld/c/ch-2.c b/challenge-175/laurent-rosenfeld/c/ch-2.c
new file mode 100644
index 0000000000..3aabbdabda
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/c/ch-2.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#define MAX_VAL 50000
+
+int cache[MAX_VAL];
+
+int gcd(int i, int j) {
+ while (j != 0) {
+ int temp = i % j;
+ i = j;
+ j = temp;
+ }
+ return i;
+}
+
+int is_perfect_totient (int num) {
+ int tot = 0;
+ for (int i = 1; i < num; i++) {
+ if (gcd(num, i) == 1) {
+ tot++;
+ }
+ }
+ int sum = tot + cache[tot];
+ cache[num] = sum;
+ return num == sum;
+}
+
+int main() {
+ int j = 1;
+ int count = 0;
+ while (count < 20) {
+ if (is_perfect_totient(j)) {
+ printf("%d ", j);
+ count++;
+ }
+ j++;
+ }
+ printf("%s\n", " ");
+}
diff --git a/challenge-175/laurent-rosenfeld/d/ch-2.d b/challenge-175/laurent-rosenfeld/d/ch-2.d
new file mode 100644
index 0000000000..5bd0fd30a1
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/d/ch-2.d
@@ -0,0 +1,29 @@
+import std.stdio;
+import std.numeric;
+
+int[10000] cache;
+
+bool is_perfect_totient(int num) {
+ int tot = 0;
+ for (int i = 1; i < num; i++) {
+ if (gcd(num, i) == 1) {
+ tot++;
+ }
+ }
+ int sum = tot + cache[tot];
+ cache[num] = sum;
+ return num == sum;
+}
+
+void main() {
+ int j = 1;
+ int count = 0;
+ while (count < 20) {
+ if (is_perfect_totient(j)) {
+ printf("%d ", j);
+ count++;
+ }
+ j++;
+ }
+ writeln(" ");
+}
diff --git a/challenge-175/laurent-rosenfeld/go/ch-2.go b/challenge-175/laurent-rosenfeld/go/ch-2.go
new file mode 100644
index 0000000000..5fd36c7d8a
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/go/ch-2.go
@@ -0,0 +1,37 @@
+import "fmt"
+
+var cache [10000]int
+
+func gcd(i int, j int) int {
+ for j != 0 {
+ temp := i % j
+ i = j
+ j = temp
+ }
+ return i
+}
+
+func is_perfect_totient(n int) bool {
+ tot := 0
+ for i := 1; i < n; i++ {
+ if gcd(n, i) == 1 {
+ tot++
+ }
+ }
+ sum := tot + cache[tot]
+ cache[n] = sum
+ return n == sum
+}
+
+func main() {
+ i := 0
+ count := 0
+ for count <= 20 {
+ if is_perfect_totient(i) {
+ fmt.Printf("%d ", i)
+ count++
+ }
+ i++
+ }
+ fmt.Println("")
+}
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");
+ }
+}
diff --git a/challenge-175/laurent-rosenfeld/javascript/ch-2.js b/challenge-175/laurent-rosenfeld/javascript/ch-2.js
new file mode 100644
index 0000000000..7d837c23cb
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/javascript/ch-2.js
@@ -0,0 +1,35 @@
+var cache = new Array(10000)
+cache[0] = 0
+
+function gcd (i, j) {
+ while(j != 0) {
+ k = j
+ j = i % j
+ i = k
+ }
+ return i
+}
+
+function is_perfect_totient (n) {
+ let tot = 0
+ for (var i = 1; i < n; i++) {
+ if (gcd(n, i) == 1) {
+ tot++
+ }
+ }
+ sum = tot + cache[tot]
+ cache[n] = sum
+ return n == sum
+}
+
+let count = 0
+let i = 1
+while (count < 20) {
+ if (is_perfect_totient(i)) {
+ process.stdout.write(i + " ")
+
+ count++
+ }
+ i++
+}
+process.stdout.write("\n")
diff --git a/challenge-175/laurent-rosenfeld/julia/ch-1.jl b/challenge-175/laurent-rosenfeld/julia/ch-1.jl
new file mode 100644
index 0000000000..7c869b02cd
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/julia/ch-1.jl
@@ -0,0 +1,12 @@
+using Dates
+
+function sundays(year, month)
+ month_end = Dates.lastdayofmonth(Dates.Date(year, month, 1))
+ weekday = Dates.dayofweek(month_end)
+ println(month_end - Dates.Day(weekday % 7))
+end
+
+year = parse( Int, ARGS[1])
+for month in 1:12
+ sundays(year, month)
+end
diff --git a/challenge-175/laurent-rosenfeld/julia/ch-2.jl b/challenge-175/laurent-rosenfeld/julia/ch-2.jl
new file mode 100644
index 0000000000..91ffaa7aa9
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/julia/ch-2.jl
@@ -0,0 +1,19 @@
+function is_perfect_totient(num)
+ n = num
+ sum = 0
+ while n >= 1
+ n = length( filter((x) -> gcd(x, n) == 1, 1:n-1))
+ sum += n
+ end
+ return num == sum
+end
+
+count = 0
+n = 1
+while count < 20
+ if is_perfect_totient(n)
+ print("$n ")
+ global count += 1
+ end
+ global n += 1;
+end
diff --git a/challenge-175/laurent-rosenfeld/kotlin/ch-2.kt b/challenge-175/laurent-rosenfeld/kotlin/ch-2.kt
new file mode 100644
index 0000000000..7616fe9219
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/kotlin/ch-2.kt
@@ -0,0 +1,37 @@
+val cache = Array(10000, {i-> 0})
+
+fun gcd (m: Int, n: Int): Int {
+ var i = m
+ var j = n
+ while(j != 0) {
+ val k = j
+ j = i % j
+ i = k
+ }
+ return i
+}
+
+fun is_perfect_totient(n: Int): Boolean {
+ var tot = 0
+ for (i in 1..n-1) {
+ if (gcd(n, i) == 1) {
+ tot++
+ }
+ }
+ val sum = tot + cache[tot]
+ cache[n] = sum
+ return n == sum
+}
+
+fun main() {
+ var i = 0
+ var count = 0
+ while (count <= 20) {
+ if (is_perfect_totient(i)) {
+ print("$i ")
+ count++
+ }
+ i++
+ }
+ println(" ")
+}
diff --git a/challenge-175/laurent-rosenfeld/nim/ch-2.nim b/challenge-175/laurent-rosenfeld/nim/ch-2.nim
new file mode 100644
index 0000000000..3bc982cf89
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/nim/ch-2.nim
@@ -0,0 +1,21 @@
+import math
+
+var cache: array[0..10000, int]
+
+proc is_perfect_totient (n: int): bool =
+ var tot = 0
+ for i in 1..n-1:
+ if (gcd(n, i) == 1):
+ tot += 1
+ let sum = tot + cache[tot]
+ cache[n] = sum
+ return sum == n
+
+var i = 1
+var count = 0
+while count < 20:
+ if is_perfect_totient(i):
+ stdout.write i, " "
+ count += 1
+ i += 1
+echo ""
diff --git a/challenge-175/laurent-rosenfeld/python/ch-1.py b/challenge-175/laurent-rosenfeld/python/ch-1.py
new file mode 100644
index 0000000000..78e40c3d33
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/python/ch-1.py
@@ -0,0 +1,23 @@
+from datetime import date,timedelta
+import sys
+
+def lastsundays (y):
+ for m in range(1,13):
+ if m == 12:
+ year = y + 1
+ month = 1
+ else:
+ year = y
+ month = m + 1
+
+ mthEnd = date(year, month, 1) - timedelta(days = 1)
+ weekDay = mthEnd.weekday()
+ lastSun = mthEnd - timedelta(days = (weekDay + 1) % 7)
+ print(lastSun)
+
+if len(sys.argv) == 2:
+ year = int(sys.argv[1])
+else:
+ year = 2022
+
+lastsundays(year)
diff --git a/challenge-175/laurent-rosenfeld/python/ch-2.py b/challenge-175/laurent-rosenfeld/python/ch-2.py
new file mode 100644
index 0000000000..d465124687
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/python/ch-2.py
@@ -0,0 +1,13 @@
+import math
+
+cache = [0] * 10000
+
+def is_perfect_totient (n):
+ tot = 0
+ for i in range(1, n):
+ if (math.gcd(n, i) == 1):
+ tot += 1
+
+​ sum = tot + cache[tot] ​ cache[n] = sum ​ return n == sum
+​
+i = 1 ​ count = 0 ​ while count < 20: ​ if isperfecttotient(i): ​ print(i, end = ” “) ​ count += 1 ​ i += 1 ​ print(” “)
diff --git a/challenge-175/laurent-rosenfeld/ring/ch-2.ring b/challenge-175/laurent-rosenfeld/ring/ch-2.ring
new file mode 100644
index 0000000000..a0c394adac
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/ring/ch-2.ring
@@ -0,0 +1,33 @@
+t_start = clock()
+j = 1
+count = 0
+cache = list(10000)
+while count < 14
+ if is_perfect_totient(j)
+ see "" + j + " "
+ count++
+ ok
+ j++
+end
+see nl
+duration = (clock() - t_start)/clockspersecond()
+see "" + duration + nl
+
+func gcd (i, j)
+ while j != 0
+ k = i % j
+ i = j
+ j = k
+ end
+ return i
+
+func is_perfect_totient (num)
+ tot = 0
+ for i = 1 to (num-1)
+ if gcd(num, i) = 1
+ tot++
+ ok
+ next
+ sum = tot + cache[tot+1]
+ cache[num+1] = sum
+ return num = sum
diff --git a/challenge-175/laurent-rosenfeld/ruby/ch-1.rb b/challenge-175/laurent-rosenfeld/ruby/ch-1.rb
new file mode 100644
index 0000000000..e92135e413
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/ruby/ch-1.rb
@@ -0,0 +1,9 @@
+require 'date'
+
+year = ARGV.shift.to_i.nil? || 2022
+
+for month in 1..12
+ lmd = Date.new(year, month, 1).next_month.prev_day
+ weekday = lmd.wday
+ puts lmd - (weekday % 7)
+end
diff --git a/challenge-175/laurent-rosenfeld/rust/ch-2.rs b/challenge-175/laurent-rosenfeld/rust/ch-2.rs
new file mode 100644
index 0000000000..dd4c284f75
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/rust/ch-2.rs
@@ -0,0 +1,30 @@
+use num::integer::gcd;
+
+static mut CACHE:[i32;10000] = [0; 10000];
+
+fn is_perfect_totient(n: i32) -> bool {
+ let mut tot = 0;
+ for i in 1..n {
+ if gcd(n, i) == 1 {
+ tot += 1
+ }
+ }
+ unsafe {
+ let sum = tot + CACHE[tot as usize];
+ CACHE[n as usize] = sum;
+ return n == sum;
+ }
+}
+
+fn main() {
+ let mut i = 1;
+ let mut count = 0;
+ while count < 20 {
+ if is_perfect_totient(i) {
+ print!("{} ", i);
+ count += 1;
+ }
+ i += 1;
+ }
+ println!("{}", " ")
+}