diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-29 00:13:18 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-29 00:13:18 +0000 |
| commit | fdf926e7b9a0ad6327f8a21d76f5ce68aa59ad97 (patch) | |
| tree | 62729acab573d86465b657e93948a26fe662626d /challenge-003 | |
| parent | a3086180855a10fc5e25ef0ff84530908cc77bcd (diff) | |
| parent | 94e384b4b3ad6812bc5d36ad1fc56456f029e0cd (diff) | |
| download | perlweeklychallenge-club-fdf926e7b9a0ad6327f8a21d76f5ce68aa59ad97.tar.gz perlweeklychallenge-club-fdf926e7b9a0ad6327f8a21d76f5ce68aa59ad97.tar.bz2 perlweeklychallenge-club-fdf926e7b9a0ad6327f8a21d76f5ce68aa59ad97.zip | |
Merge pull request #3402 from Abigail/abigail/week-003
Abigail/week 003
Diffstat (limited to 'challenge-003')
27 files changed, 1021 insertions, 1 deletions
diff --git a/challenge-003/abigail/README b/challenge-003/abigail/README deleted file mode 100644 index 5f0d73ae16..0000000000 --- a/challenge-003/abigail/README +++ /dev/null @@ -1 +0,0 @@ -Solution by Abigail diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md new file mode 100644 index 0000000000..586b7ee14e --- /dev/null +++ b/challenge-003/abigail/README.md @@ -0,0 +1,47 @@ +# Solution by Abigail + +## [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 +* [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) +* [bc](bc/ch-1.bc) +* [C](c/ch-1.c) +* [Lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) +* [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) + + +## [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 +* [AWK](awk/ch-2.awk) +* [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) +* [Node.js](node/ch-2.js) +* [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.rb) diff --git a/challenge-003/abigail/awk/ch-1.awk b/challenge-003/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..706014ed91 --- /dev/null +++ b/challenge-003/abigail/awk/ch-1.awk @@ -0,0 +1,29 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# 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 +# +{ + for (base2 = 1; base2 <= $0; base2 *= 2) { + for (base3 = base2; base3 <= $0; base3 *= 3) { + for (base5 = base3; base5 <= $0; base5 *= 5) { + print base5 + } + } + } +} diff --git a/challenge-003/abigail/awk/ch-2.awk b/challenge-003/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..1689ce668f --- /dev/null +++ b/challenge-003/abigail/awk/ch-2.awk @@ -0,0 +1,33 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + row [1] = 1 # 0'th row + printf "%d\n", row [1] + + for (r = 1; r <= $0; r ++) { + # + # Calculate the new row, and print it + # + for (i = 1; i <= length (row) + 1; i ++) { + new [i] = (i == 1 ? 0 : row [i - 1]) +\ + (i == length (row) + 1 ? 0 : row [i]) + printf ("%s%d", i == 1 ? "" : " ", new [i]) + } + printf "\n"; + + # + # Copy the new row to the current row + # + for (i = 1; i <= length (new); i ++) { + row [i] = new [i] + } + } +} diff --git a/challenge-003/abigail/bash/ch-1.sh b/challenge-003/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..76029e700b --- /dev/null +++ b/challenge-003/abigail/bash/ch-1.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +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 + done +done diff --git a/challenge-003/abigail/bc/ch-1.bc b/challenge-003/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..d3336b509d --- /dev/null +++ b/challenge-003/abigail/bc/ch-1.bc @@ -0,0 +1,8 @@ +max = read () +for (base2 = 1; base2 <= max; base2 *= 2) { + for (base3 = base2; base3 <= max; base3 *= 3) { + for (base5 = base3; base5 <= max; base5 *= 5) { + base5 + } + } +} diff --git a/challenge-003/abigail/c/ch-1.c b/challenge-003/abigail/c/ch-1.c new file mode 100644 index 0000000000..eb6a053705 --- /dev/null +++ b/challenge-003/abigail/c/ch-1.c @@ -0,0 +1,31 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +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); + } + } + } + } + free (line); + + return (0); +} diff --git a/challenge-003/abigail/c/ch-2.c b/challenge-003/abigail/c/ch-2.c new file mode 100644 index 0000000000..805f0d93be --- /dev/null +++ b/challenge-003/abigail/c/ch-2.c @@ -0,0 +1,69 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t strlen; + + while ((strlen = getline (&line, &len, stdin)) != -1) { + int rows = atoi (line); + /* + * Declare two rows, and malloc memory for thew + */ + long long * row; + long long * new; + + if ((row = (long long *) malloc ((rows + 1) * sizeof (long long))) + == NULL) { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + + if ((new = (long long *) malloc ((rows + 1) * sizeof (long long))) + == NULL) { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + + /* + * 0th row + */ + row [0] = 1; + printf ("%lld\n", row [0]); + + for (int r = 1; r <= rows; r ++) { + /* + * Create the next row, and print it + */ + for (int i = 0; i <= r; i ++) { + new [i] = (i == 0 ? 0 : row [i - 1]) + + (i == r ? 0 : row [i]); + printf ("%s%lld", i == 0 ? "" : " ", new [i]); + } + printf ("\n"); + + /* + * Swap the two roles + */ + long long * tmp = new; + new = row; + row = tmp; + } + + free (new); + free (row); + } + free (line); + + return (0); +} diff --git a/challenge-003/abigail/lua/ch-1.lua b/challenge-003/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..70de9b1923 --- /dev/null +++ b/challenge-003/abigail/lua/ch-1.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- 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 + end + base2 = base2 * 2 + end +end diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..0e30b0a9c6 --- /dev/null +++ b/challenge-003/abigail/lua/ch-2.lua @@ -0,0 +1,51 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + + +-- +-- Iterate over the input +-- +for line in io . lines () do + local rows = tonumber (line) + + -- + -- Create and print row 0 + -- + local row = {1} + print "1" + + local r + for r = 1, rows do + -- + -- Create a new row + -- + local new = {} + for i = 1, r + 1 do + sum = 0; + if i > 1 + then sum = sum + row [i - 1] + io . write (" ") + end + if i <= r + then sum = sum + row [i] + end + -- + -- Assign new element, and print it + -- + new [i] = sum + io . write (sum) + end + io . write ("\n") + -- + -- New row becomes current row + -- + row = new + end +end diff --git a/challenge-003/abigail/node/ch-1.js b/challenge-003/abigail/node/ch-1.js new file mode 100644 index 0000000000..a1ffe969ac --- /dev/null +++ b/challenge-003/abigail/node/ch-1.js @@ -0,0 +1,21 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +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) + } + } + } +}); diff --git a/challenge-003/abigail/node/ch-2.js b/challenge-003/abigail/node/ch-2.js new file mode 100644 index 0000000000..af1724bb18 --- /dev/null +++ b/challenge-003/abigail/node/ch-2.js @@ -0,0 +1,43 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => triangle (+_)) +; + + +function triangle (rows) { + // + // Create and print the 0th row + // + let row = [1] + process . stdout . write ("1\n") + for (let r = 1; r <= rows; r ++) { + // + // Calculate a new row + // + let new_row = [] + for (i = 0; i <= r; i ++) { + new_row [i] = (i == 0 ? 0 : row [i - 1]) + + (i == r ? 0 : row [i]); + if (i > 0) { + process . stdout . write (" ") + } + process . stdout . write ("" + new_row [i]) + } + process . stdout . write ("\n") + + // + // New row becomes current row + // + row = new_row + } +} diff --git a/challenge-003/abigail/perl/ch-1.pl b/challenge-003/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..352466e41c --- /dev/null +++ b/challenge-003/abigail/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# Read the maximum number from STDIN +# +chomp (my $MAX = <>); + +# +# Generate the 5-smooth numbers up to $MAX. +# 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 +# +for (my $base2 = 1; $base2 <= $MAX; $base2 *= 2) { + for (my $base3 = $base2; $base3 <= $MAX; $base3 *= 3) { + for (my $base5 = $base3; $base5 <= $MAX; $base5 *= 5) { + say $base5; + } + } +} diff --git a/challenge-003/abigail/perl/ch-2.pl b/challenge-003/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..d5c14c4701 --- /dev/null +++ b/challenge-003/abigail/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +while (my $rows = <>) { + chomp $rows; + + # + # 0-th row + # + my @row = (1); + say "@row"; + + foreach (1 .. $rows) { + # + # Calculate the next row from the current row + # + my @new = map {($_ == 0 ? 0 : $row [$_ - 1]) + + ($_ == @row ? 0 : $row [$_])} 0 .. @row; + + # + # Print + # + say "@new"; + + # + # New row becomes current row + # + @row = @new; + } +} diff --git a/challenge-003/abigail/python/ch-1.py b/challenge-003/abigail/python/ch-1.py new file mode 100644 index 0000000000..30b41dd8fe --- /dev/null +++ b/challenge-003/abigail/python/ch-1.py @@ -0,0 +1,27 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + max = int (line) + # + # Python does not have a for (;;) style loop + # + base2 = 1 + while base2 <= max: + base3 = base2 + while base3 <= max: + base5 = base3 + while base5 <= max: + print base5 + base5 *= 5 + base3 *= 3 + base2 *= 2 diff --git a/challenge-003/abigail/python/ch-2.py b/challenge-003/abigail/python/ch-2.py new file mode 100644 index 0000000000..9370561d8b --- /dev/null +++ b/challenge-003/abigail/python/ch-2.py @@ -0,0 +1,46 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-2.py < input-file +# + +import fileinput +import sys + +# +# Iterate over the input +# +for line in fileinput . input (): + rows = int (line) + + # + # Create the first row, and print it + # + row = [1] + sys . stdout . write (str (1) + "\n") + + for r in range (1, rows + 1): + # + # Create a new row + # + new = [None] * (r + 1) # In Python, arrays don't grow automatically + for i in range (r + 1): + sum = 0 + if i > 0: + sum = row [i - 1] + sys . stdout . write (" ") + if i < r: + sum = sum + row [i] + new [i] = sum + sys . stdout . write (str (sum)) + sys . stdout . write ("\n") + + # + # New row becomes current row + # + row = new + diff --git a/challenge-003/abigail/ruby/ch-1.rb b/challenge-003/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..c0a22397b0 --- /dev/null +++ b/challenge-003/abigail/ruby/ch-1.rb @@ -0,0 +1,26 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |_| + max = _ . to_i + base2 = 1 + while base2 <= max + base3 = base2 + while base3 <= max + base5 = base3 + while base5 <= max + puts base5 + base5 *= 5 + end + base3 *= 3 + end + base2 *= 2 + end +end diff --git a/challenge-003/abigail/ruby/ch-2.rb b/challenge-003/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..85736b99cb --- /dev/null +++ b/challenge-003/abigail/ruby/ch-2.rb @@ -0,0 +1,39 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do |_| + rows = _ . to_i + + # + # 0-th row + # + row = [1] + puts row [0] + for r in 1 .. rows do + # + # Create new row + # + new = [] + for i in 0 .. r do + new [i] = (i == 0 ? 0 : row [i - 1]) + + (i == r ? 0 : row [i]) + if i > 0 + then print " " + end + print new [i] + end + puts "" + + # + # New row becomes current row + # + row = new + end +end diff --git a/challenge-003/abigail/t/ctest.ini b/challenge-003/abigail/t/ctest.ini new file mode 100644 index 0000000000..959e85ac42 --- /dev/null +++ b/challenge-003/abigail/t/ctest.ini @@ -0,0 +1,14 @@ +#
+# Configuration file for running tests, using ctest.
+# See https://github.com/Abigail/Misc/blob/master/ctest
+#
+
+[names]
+1-1 = OEIS Example
+1-2 = Large input
+2-1 = Minimal triangle
+2-2 = 15 row triangle
+
+
+[1-1,1-2]
+sort = numeric
diff --git a/challenge-003/abigail/t/input-1-1 b/challenge-003/abigail/t/input-1-1 new file mode 100644 index 0000000000..ec8785ec99 --- /dev/null +++ b/challenge-003/abigail/t/input-1-1 @@ -0,0 +1 @@ +405 diff --git a/challenge-003/abigail/t/input-1-2 b/challenge-003/abigail/t/input-1-2 new file mode 100644 index 0000000000..f7393e847d --- /dev/null +++ b/challenge-003/abigail/t/input-1-2 @@ -0,0 +1 @@ +100000 diff --git a/challenge-003/abigail/t/input-2-1 b/challenge-003/abigail/t/input-2-1 new file mode 100644 index 0000000000..00750edc07 --- /dev/null +++ b/challenge-003/abigail/t/input-2-1 @@ -0,0 +1 @@ +3 diff --git a/challenge-003/abigail/t/input-2-2 b/challenge-003/abigail/t/input-2-2 new file mode 100644 index 0000000000..60d3b2f4a4 --- /dev/null +++ b/challenge-003/abigail/t/input-2-2 @@ -0,0 +1 @@ +15 diff --git a/challenge-003/abigail/t/output-1-1.exp b/challenge-003/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..04fd1a4f4b --- /dev/null +++ b/challenge-003/abigail/t/output-1-1.exp @@ -0,0 +1,62 @@ +1 +2 +3 +4 +5 +6 +8 +9 +10 +12 +15 +16 +18 +20 +24 +25 +27 +30 +32 +36 +40 +45 +48 +50 +54 +60 +64 +72 +75 +80 +81 +90 +96 +100 +108 +120 +125 +128 +135 +144 +150 +160 +162 +180 +192 +200 +216 +225 +240 +243 +250 +256 +270 +288 +300 +320 +324 +360 +375 +384 +400 +405 diff --git a/challenge-003/abigail/t/output-1-2.exp b/challenge-003/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..69a24cd064 --- /dev/null +++ b/challenge-003/abigail/t/output-1-2.exp @@ -0,0 +1,313 @@ +1 +2 +3 +4 +5 +6 +8 +9 +10 +12 +15 +16 +18 +20 +24 +25 +27 +30 +32 +36 +40 +45 +48 +50 +54 +60 +64 +72 +75 +80 +81 +90 +96 +100 +108 +120 +125 +128 +135 +144 +150 +160 +162 +180 +192 +200 +216 +225 +240 +243 +250 +256 +270 +288 +300 +320 +324 +360 +375 +384 +400 +405 +432 +450 +480 +486 +500 +512 +540 +576 +600 +625 +640 +648 +675 +720 +729 +750 +768 +800 +810 +864 +900 +960 +972 +1000 +1024 +1080 +1125 +1152 +1200 +1215 +1250 +1280 +1296 +1350 +1440 +1458 +1500 +1536 +1600 +1620 +1728 +1800 +1875 +1920 +1944 +2000 +2025 +2048 +2160 +2187 +2250 +2304 +2400 +2430 +2500 +2560 +2592 +2700 +2880 +2916 +3000 +3072 +3125 +3200 +3240 +3375 +3456 +3600 +3645 +3750 +3840 +3888 +4000 +4050 +4096 +4320 +4374 +4500 +4608 +4800 +4860 +5000 +5120 +5184 +5400 +5625 +5760 +5832 +6000 +6075 +6144 +6250 +6400 +6480 +6561 +6750 +6912 +7200 +7290 +7500 +7680 +7776 +8000 +8100 +8192 +8640 +8748 +9000 +9216 +9375 +9600 +9720 +10000 +10125 +10240 +10368 +10800 +10935 +11250 +11520 +11664 +12000 +12150 +12288 +12500 +12800 +12960 +13122 +13500 +13824 +14400 +14580 +15000 +15360 +15552 +15625 +16000 +16200 +16384 +16875 +17280 +17496 +18000 +18225 +18432 +18750 +19200 +19440 +19683 +20000 +20250 +20480 +20736 +21600 +21870 +22500 +23040 +23328 +24000 +24300 +24576 +25000 +25600 +25920 +26244 +27000 +27648 +28125 +28800 +29160 +30000 +30375 +30720 +31104 +31250 +32000 +32400 +32768 +32805 +33750 +34560 +34992 +36000 +36450 +36864 +37500 |
