diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-13 16:52:50 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-13 16:52:50 +0800 |
| commit | b82ed7c55b6a912b237e7fa396e433ac33ca79c8 (patch) | |
| tree | af2ad93bf0190b04d9c3775c87ab3dd61abd99dd /challenge-003 | |
| parent | 415b77f3f489715d7888de8794affe9afe8d1ffb (diff) | |
| parent | f0bd80b2369212d923d8c6a537ba5379067afbf9 (diff) | |
| download | perlweeklychallenge-club-b82ed7c55b6a912b237e7fa396e433ac33ca79c8.tar.gz perlweeklychallenge-club-b82ed7c55b6a912b237e7fa396e433ac33ca79c8.tar.bz2 perlweeklychallenge-club-b82ed7c55b6a912b237e7fa396e433ac33ca79c8.zip | |
-
Diffstat (limited to 'challenge-003')
38 files changed, 928 insertions, 535 deletions
diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 586b7ee14e..0438781a9f 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -1,47 +1,36 @@ -# Solution by Abigail +# Solution by Abigail, week 3 -## [Challenge #1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/#challenge-1) -Create a script to generate `5`-smooth numbers, whose prime divisors -are less or equal to `5`. They are also called Hamming/Regular/Ugly -numbers. For more information, please check this -[wikipedia](https://en.wikipedia.org/wiki/Regular_number). - -### Notes -We are going to generate all numbers `n = 2^i * 3^j * 5^k`, with -`i >= 0`, `j >= 0`, `k >= 0`, and `n <= MAX`, where `MAX` is read -from `STDIN`. No other numbers are generated. - -We are *not* going to generate the numbers `n` in -numerical order. Instead, if we have two numbers `n1 = 2^i1 * 3^j1 * 5^k1`, -and `n2 = 2^i2 * 3^j2 * 5^k2`, then `n1` is generated before `n2`, iff - - i1 < i2 || - i1 == i2 && j1 < j2 || - i1 == i2 && j1 == j2 && k1 < k2 - -### Solutions +### Part 1 * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [bc](bc/ch-1.bc) * [C](c/ch-1.c) +* [Go](go/ch-1.go) +* [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) +* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [R](r/ch-1.r) * [Ruby](ruby/ch-1.rb) +* [Scheme](scheme/ch-1.scm) +* [Tcl](tcl/ch-1.tcl) -## [Challenge #2](https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/#challenge-2) -Create a script that generates Pascal Triangle. Accept number of -rows from the command line. The Pascal Triangle should have at least -3 rows. For more information about Pascal Triangle, check this -[wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle) page. - -### Solutions +### Part 2 * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) +* [bc](bc/ch-2.bc) * [C](c/ch-2.c) +* [Go](go/ch-2.go) +* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) +* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [R](r/ch-2.r) * [Ruby](ruby/ch-2.rb) +* [Scheme](scheme/ch-2.scm) +* [Tcl](tcl/ch-2.tcl) diff --git a/challenge-003/abigail/awk/ch-1.awk b/challenge-003/abigail/awk/ch-1.awk index 706014ed91..c6fd5a3e55 100644 --- a/challenge-003/abigail/awk/ch-1.awk +++ b/challenge-003/abigail/awk/ch-1.awk @@ -1,29 +1,39 @@ #!/usr/bin/awk # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # # Run as: awk -f ch-1.awk < input-file # -# -# Generate the 5-smooth numbers up to $0. -# This does *NOT* generate the numbers is order. It does, however, -# generate all of them, and no other numbers. -# -# -# base2 is of the form 2^i; i >= 0 -# base3 if of the form 2^i * 3^j; i, j >= 0 -# base5 is of the form 2^i * 3^j * 5^k; i, j, k >= 0 -# +BEGIN { + ugly [0] = 1 + next_2 = 0 + next_3 = 0 + next_5 = 0 + count = 1 +} + { - for (base2 = 1; base2 <= $0; base2 *= 2) { - for (base3 = base2; base3 <= $0; base3 *= 3) { - for (base5 = base3; base5 <= $0; base5 *= 5) { - print base5 - } - } + while (count < $1) { + c2 = 2 * ugly [next_2] + c3 = 3 * ugly [next_3] + c5 = 5 * ugly [next_5] + + ugly [count] = c2 < c3 ? c2 < c5 ? c2 : c5 \ + : c3 < c5 ? c3 : c5 + + if (2 * ugly [next_2] <= ugly [count]) {next_2 ++} + if (3 * ugly [next_3] <= ugly [count]) {next_3 ++} + if (5 * ugly [next_5] <= ugly [count]) {next_5 ++} + + count ++ } + print ugly [$1 - 1] } + + + + diff --git a/challenge-003/abigail/awk/ch-2.awk b/challenge-003/abigail/awk/ch-2.awk index 1689ce668f..1079aaab62 100644 --- a/challenge-003/abigail/awk/ch-2.awk +++ b/challenge-003/abigail/awk/ch-2.awk @@ -1,7 +1,7 @@ #!/usr/bin/awk # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # diff --git a/challenge-003/abigail/bash/ch-1.sh b/challenge-003/abigail/bash/ch-1.sh index 76029e700b..b2f98abec9 100644 --- a/challenge-003/abigail/bash/ch-1.sh +++ b/challenge-003/abigail/bash/ch-1.sh @@ -1,7 +1,7 @@ #!/bin/sh # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # @@ -10,12 +10,28 @@ set -f -while read max -do for ((base2 = 1; $base2 <= $max; base2 *= 2)) - do for ((base3 = $base2; $base3 <= $max; base3 *= 3)) - do for ((base5 = $base3; $base5 <= $max; base5 *= 5)) - do echo $base5 - done - done +declare -a ugly +ugly[0]=1 +((next_2 = 0)) +((next_3 = 0)) +((next_5 = 0)) +count=1 + +while read n +do while ((count < n)) + do ((c2 = 2 * ${ugly[next_2]})) + ((c3 = 3 * ${ugly[next_3]})) + ((c5 = 5 * ${ugly[next_5]})) + + if ((c2 <= c3 && c2 <= c5)); then ((ugly[count] = c2)); fi + if ((c3 <= c2 && c3 <= c5)); then ((ugly[count] = c3)); fi + if ((c5 <= c3 && c5 <= c2)); then ((ugly[count] = c5)); fi + + if ((2 * ${ugly[next_2]} <= ${ugly[count]})); then ((next_2 ++)); fi + if ((3 * ${ugly[next_3]} <= ${ugly[count]})); then ((next_3 ++)); fi + if ((5 * ${ugly[next_5]} <= ${ugly[count]})); then ((next_5 ++)); fi + + ((count ++)) done + echo ${ugly[$((n - 1))]} done diff --git a/challenge-003/abigail/bash/ch-2.sh b/challenge-003/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..ca5e5fb510 --- /dev/null +++ b/challenge-003/abigail/bash/ch-2.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare -A p + +while read n +do for ((row = 0; row <= n; row ++)) + do p["$row;0"]=1 + printf "1 " + for ((col = 1; col <= row; col ++)) + do p["$row;$col"]=$((${p["$((row-1));$((col-1))"]:-0} + \ + ${p["$((row-1));$col"]:-0})) + printf "%d " ${p["$row;$col"]} + done + echo + done +done diff --git a/challenge-003/abigail/bc/ch-1.bc b/challenge-003/abigail/bc/ch-1.bc index d3336b509d..bbbbdf3a2e 100644 --- a/challenge-003/abigail/bc/ch-1.bc +++ b/challenge-003/abigail/bc/ch-1.bc @@ -1,8 +1,35 @@ -max = read () -for (base2 = 1; base2 <= max; base2 *= 2) { - for (base3 = base2; base3 <= max; base3 *= 3) { - for (base5 = base3; base5 <= max; base5 *= 5) { - base5 - } +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: bc ch-1.bc < input-file +# + +while (1) { + n = read () + if (n == 0) { + break } + ugly [0] = 1 + next_2 = 0 + next_3 = 0 + next_5 = 0 + index = 1 + while (index < n) { + u2 = 2 * ugly [next_2] + u3 = 3 * ugly [next_3] + u5 = 5 * ugly [next_5] + if ((u2 <= u3) && (u2 <= u5)) {min = u2} + if ((u3 <= u2) && (u3 <= u5)) {min = u3} + if ((u5 <= u2) && (u5 <= u3)) {min = u5} + ugly [index] = min + if (2 * ugly [next_2] <= ugly [index]) {next_2 = next_2 + 1} + if (3 * ugly [next_3] <= ugly [index]) {next_3 = next_3 + 1} + if (5 * ugly [next_5] <= ugly [index]) {next_5 = next_5 + 1} + index = index + 1 + } + ugly [index - 1] } diff --git a/challenge-003/abigail/bc/ch-2.bc b/challenge-003/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..e2c41b0455 --- /dev/null +++ b/challenge-003/abigail/bc/ch-2.bc @@ -0,0 +1,35 @@ +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: bc ch-2.bc < input-file +# + +while (1) { + n = read () + if (n == 0) { + break + } + + p [0] = 1 + p [0] + for (i = 1; i <= n; i ++) { + for (j = 0; j < i; j ++) { + n [j] = p [j] + if (j - 1 >= 0) { + n [j] = n [j] + p [j - 1] + } + print n [j], " " + } + n [i] = 1 + print "1 +" + for (j = 0; j <= i; j ++) { + p [j] = n [j] + } + } + break +} diff --git a/challenge-003/abigail/c/ch-1.c b/challenge-003/abigail/c/ch-1.c index eb6a053705..a5a503cb32 100644 --- a/challenge-003/abigail/c/ch-1.c +++ b/challenge-003/abigail/c/ch-1.c @@ -3,29 +3,51 @@ # include <string.h> /* - * See ../README.md + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 */ /* * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file */ +typedef long long number; int main (void) { - char * line = NULL; - size_t len = 0; - size_t strlen; - - while ((strlen = getline (&line, &len, stdin)) != -1) { - long long max = atoll (line); - for (long long base2 = 1; base2 <= max; base2 *= 2) { - for (long long base3 = base2; base3 <= max; base3 *= 3) { - for (long long base5 = base3; base5 <= max; base5 *= 5) { - printf ("%lld\n", base5); - } + int n; + number * ugly = NULL; + size_t count = 0; + size_t next_2, next_3, next_5; + + + while (scanf ("%d", &n) == 1) { + if (n > count) { + if ((ugly = (number *) realloc (ugly, n * sizeof (number))) + == NULL) { + perror ("Realloc failed"); + exit (1); } } + if (count == 0) { + ugly [0] = 1; + count = 1; + next_2 = next_3 = next_5 = 0; + } + while (count < n) { + number c2 = 2 * ugly [next_2]; + number c3 = 3 * ugly [next_3]; + number c5 = 5 * ugly [next_5]; + + ugly [count] = c2 < c3 ? c2 < c5 ? c2 : c5 + : c3 < c5 ? c3 : c5; + + if (2 * ugly [next_2] <= ugly [count]) {next_2 ++;} + if (3 * ugly [next_3] <= ugly [count]) {next_3 ++;} + if (5 * ugly [next_5] <= ugly [count]) {next_5 ++;} + + count ++; + } + printf ("%lld\n", ugly [n - 1]); } - free (line); + free (ugly); return (0); } diff --git a/challenge-003/abigail/c/ch-2.c b/challenge-003/abigail/c/ch-2.c index 805f0d93be..7dee8c4eff 100644 --- a/challenge-003/abigail/c/ch-2.c +++ b/challenge-003/abigail/c/ch-2.c @@ -3,7 +3,7 @@ # include <string.h> /* - * See ../README.md + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 */ /* diff --git a/challenge-003/abigail/go/ch-1.go b/challenge-003/abigail/go/ch-1.go new file mode 100644 index 0000000000..03229cb964 --- /dev/null +++ b/challenge-003/abigail/go/ch-1.go @@ -0,0 +1,47 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// Run as: go run ch-1.go < input-file +// + +import ( + "fmt" +) + +func main () { + var max int + for { + n, err := fmt . Scanf ("%d", &max) + if (n != 1 || err != nil) { + break + } + ugly := make ([] int, max) + ugly [0] = 1 + count := 0 + next_2 := 0 + next_3 := 0 + next_5 := 0 + min := 0 + + for count < max - 1 { + count ++ + + c2 := 2 * ugly [next_2] + c3 := 3 * ugly [next_3] + c5 := 5 * ugly [next_5] + if c2 <= c3 && c2 <= c5 {min = c2} + if c3 <= c2 && c3 <= c5 {min = c3} + if c5 <= c2 && c5 <= c3 {min = c5} + ugly [count] = min + + if (c2 <= ugly [count]) {next_2 ++} + if (c3 <= ugly [count]) {next_3 ++} + if (c5 <= ugly [count]) {next_5 ++} + } + fmt . Println (ugly [count]) + } +} diff --git a/challenge-003/abigail/go/ch-2.go b/challenge-003/abigail/go/ch-2.go new file mode 100644 index 0000000000..d66b10dec9 --- /dev/null +++ b/challenge-003/abigail/go/ch-2.go @@ -0,0 +1,40 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" +) + +func main () { + for { + var max int + n, err := fmt . Scanf ("%d", &max) + if n != 1 || err != nil { + break + } + + current_row := make ([] int, 1) + current_row [0] = 1; + fmt . Printf ("1\n") + + for row := 1; row <= max; row ++ { + next_row := make ([] int, row + 1) + next_row [0] = 1 + next_row [row] = 1 + fmt . Printf ("1 ") + for col := 1; col < row; col ++ { + next_row [col] = current_row [col - 1] + current_row [col] + fmt . Printf ("%d ", next_row [col]) + } + fmt . Printf ("1\n") + current_row = next_row + } + } +} diff --git a/challenge-003/abigail/java/ch-1.java b/challenge-003/abigail/java/ch-1.java new file mode 100644 index 0000000000..651f938314 --- /dev/null +++ b/challenge-003/abigail/java/ch-1.java @@ -0,0 +1,46 @@ +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + int max = scanner . nextInt (); + long [] ugly = new long [max]; + + ugly [0] = 1L; + int count = 0; + int next_2 = 0; + int next_3 = 0; + int next_5 = 0; + + while (count < max - 1) { + count ++; + long min = 0; + + long c2 = 2 * ugly [next_2]; + long c3 = 3 * ugly [next_3]; + long c5 = 5 * ugly [next_5]; + + if (c2 <= c3 && c2 <= c5) {min = c2;} + if (c3 <= c2 && c3 <= c5) {min = c3;} + if (c5 <= c2 && c5 <= c3) {min = c5;} + + ugly [count] = min; + + if (c2 <= ugly [count]) {next_2 ++;} + if (c3 <= ugly [count]) {next_3 ++;} + if (c5 <= ugly [count]) {next_5 ++;} + } + + System . out . println (ugly [count]); + } + } +} diff --git a/challenge-003/abigail/java/ch-2.java b/challenge-003/abigail/java/ch-2.java new file mode 100644 index 0000000000..8f932a81db --- /dev/null +++ b/challenge-003/abigail/java/ch-2.java @@ -0,0 +1,34 @@ +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; + +public class ch2 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + int max = scanner . nextInt (); + int [] current_row = new int [1]; + current_row [0] = 1; + System . out . println ("1"); + + for (int row = 1; row <= max; row ++) { + int [] next_row = new int [row + 1]; + next_row [0] = 1; + next_row [row] = 1; + System . out . print ("1 "); + for (int col = 1; col < row; col ++) { + next_row [col] = current_row [col - 1] + current_row [col]; + System . out . printf ("%d ", next_row [col]); + } + System . out . println ("1"); + current_row = next_row; + } + } + } +} diff --git a/challenge-003/abigail/lua/ch-1.lua b/challenge-003/abigail/lua/ch-1.lua index 70de9b1923..3f0bf5b9fe 100644 --- a/challenge-003/abigail/lua/ch-1.lua +++ b/challenge-003/abigail/lua/ch-1.lua @@ -1,30 +1,48 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 -- -- -- Run as: lua ch-1.lua < input-file -- -for max in io . lines () do - local max = tonumber (max) - local base2 = 1 - -- - -- Lua doesn't have a for (expr; expr; expr) syntax. - -- This is missed here. - -- - while base2 <= max do - local base3 = base2 - while base3 <= max do - local base5 = base3 - while base5 <= max do - print (base5) - base5 = base5 * 5 - end - base3 = base3 * 3 +local ugly = {} +ugly [1] = 1 +local next_2 = 1 +local next_3 = 1 +local next_5 = 1 +local count = 1 + +for n in io . lines () do + n = tonumber (n) + while count < n do + local c2 = 2 * ugly [next_2] + local c3 = 3 * ugly [next_3] + local c5 = 5 * ugly [next_5] + + count = count + 1 + + if c2 <= c3 and c2 <= c5 then + ugly [count] = c2 + end + if c3 <= c2 and c3 <= c5 then + ugly [count] = c3 + end + if c5 <= c2 and c5 <= c3 then + ugly [count] = c5 + end + + if 2 * ugly [next_2] <= ugly [count] then + next_2 = next_2 + 1 + end + if 3 * ugly [next_3] <= ugly [count] then + next_3 = next_3 + 1 + end + if 5 * ugly [next_5] <= ugly [count] then + next_5 = next_5 + 1 end - base2 = base2 * 2 end + print (ugly [n]) end diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua index 0e30b0a9c6..aed77c8e32 100644 --- a/challenge-003/abigail/lua/ch-2.lua +++ b/challenge-003/abigail/lua/ch-2.lua @@ -1,7 +1,7 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 -- -- diff --git a/challenge-003/abigail/node/ch-1.js b/challenge-003/abigail/node/ch-1.js index a1ffe969ac..e44318f34c 100644 --- a/challenge-003/abigail/node/ch-1.js +++ b/challenge-003/abigail/node/ch-1.js @@ -1,21 +1,30 @@ #!/usr/local/bin/node // -// See ../README.md +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 // // // Run as: node ch-1.js < input-file // -require ('readline') +let ugly = [1] +let next_2 = 0 +let next_3 = 0 +let next_5 = 0 + + require ('readline') . createInterface ({input: process . stdin}) -. on ('line', max => { - for (let base2 = 1; base2 <= max; base2 *= 2) { - for (let base3 = base2; base3 <= max; base3 *= 3) { - for (let base5 = base3; base5 <= max; base5 *= 5) { - console . log (base5) - } - } +. on ('line', n => { + n =+ n + while (ugly . length < n) { + ugly . push (Math . min (2 * ugly [next_2], + 3 * ugly [next_3], + 5 * ugly [next_5])) + + if (2 * ugly [next_2] <= ugly [ugly . length - 1]) {next_2 ++} + if (3 * ugly [next_3] <= ugly [ugly . length - 1]) {next_3 ++} + if (5 * ugly [next_5] <= ugly [ugly . length - 1]) {next_5 ++} } -}); + console . log (ugly [n - 1]) +}) diff --git a/challenge-003/abigail/node/ch-2.js b/challenge-003/abigail/node/ch-2.js index af1724bb18..a161017ede 100644 --- a/challenge-003/abigail/node/ch-2.js +++ b/challenge-003/abigail/node/ch-2.js @@ -1,7 +1,7 @@ #!/usr/local/bin/node // -// See ../README.md +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 // // diff --git a/challenge-003/abigail/pascal/ch-1.p b/challenge-003/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..79eec3a1d3 --- /dev/null +++ b/challenge-003/abigail/pascal/ch-1.p @@ -0,0 +1,48 @@ +Program ch1; + +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var + max, count, next_2, next_3, next_5: integer; + min, c2, c3, c5: qword; + ugly: array of qword; + +begin + while not eof do begin + readln (max); + setlength (ugly, max); + + ugly [0] := 1; + count := 0; + next_2 := 0; + next_3 := 0; + next_5 := 0; + min := 0; + + while count < max - 1 do begin + inc (count); + + c2 := 2 * ugly [next_2]; + c3 := 3 * ugly [next_ |
