From 925d4a68cbfd366160835a465ea3dacc774c6203 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 30 Jul 2022 16:21:35 +0100 Subject: - Added guest solutions by Laurent Rosenfeld. --- challenge-175/laurent-rosenfeld/awk/ch-2.awk | 31 ++++++++++++++++++ challenge-175/laurent-rosenfeld/bc/ch-2.bc | 33 +++++++++++++++++++ challenge-175/laurent-rosenfeld/c/ch-2.c | 38 ++++++++++++++++++++++ challenge-175/laurent-rosenfeld/d/ch-2.d | 29 +++++++++++++++++ challenge-175/laurent-rosenfeld/go/ch-2.go | 37 +++++++++++++++++++++ .../laurent-rosenfeld/java/PerfectTotient.java | 37 +++++++++++++++++++++ challenge-175/laurent-rosenfeld/javascript/ch-2.js | 35 ++++++++++++++++++++ challenge-175/laurent-rosenfeld/julia/ch-1.jl | 12 +++++++ challenge-175/laurent-rosenfeld/julia/ch-2.jl | 19 +++++++++++ challenge-175/laurent-rosenfeld/kotlin/ch-2.kt | 37 +++++++++++++++++++++ challenge-175/laurent-rosenfeld/nim/ch-2.nim | 21 ++++++++++++ challenge-175/laurent-rosenfeld/python/ch-1.py | 23 +++++++++++++ challenge-175/laurent-rosenfeld/python/ch-2.py | 13 ++++++++ challenge-175/laurent-rosenfeld/ring/ch-2.ring | 33 +++++++++++++++++++ challenge-175/laurent-rosenfeld/ruby/ch-1.rb | 9 +++++ challenge-175/laurent-rosenfeld/rust/ch-2.rs | 30 +++++++++++++++++ 16 files changed, 437 insertions(+) create mode 100644 challenge-175/laurent-rosenfeld/awk/ch-2.awk create mode 100644 challenge-175/laurent-rosenfeld/bc/ch-2.bc create mode 100644 challenge-175/laurent-rosenfeld/c/ch-2.c create mode 100644 challenge-175/laurent-rosenfeld/d/ch-2.d create mode 100644 challenge-175/laurent-rosenfeld/go/ch-2.go create mode 100644 challenge-175/laurent-rosenfeld/java/PerfectTotient.java create mode 100644 challenge-175/laurent-rosenfeld/javascript/ch-2.js create mode 100644 challenge-175/laurent-rosenfeld/julia/ch-1.jl create mode 100644 challenge-175/laurent-rosenfeld/julia/ch-2.jl create mode 100644 challenge-175/laurent-rosenfeld/kotlin/ch-2.kt create mode 100644 challenge-175/laurent-rosenfeld/nim/ch-2.nim create mode 100644 challenge-175/laurent-rosenfeld/python/ch-1.py create mode 100644 challenge-175/laurent-rosenfeld/python/ch-2.py create mode 100644 challenge-175/laurent-rosenfeld/ring/ch-2.ring create mode 100644 challenge-175/laurent-rosenfeld/ruby/ch-1.rb create mode 100644 challenge-175/laurent-rosenfeld/rust/ch-2.rs 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 +#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!("{}", " ") +} -- cgit