diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-25 02:19:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-25 02:19:22 +0100 |
| commit | 1c59c21ec7e7a43ae44f587a7d279b90a31fbb62 (patch) | |
| tree | 0040d7355d7e9464ce4515f647b923213d28b076 | |
| parent | e761d4717321a14d02a3ede087ac6c0c396c155b (diff) | |
| parent | 078dca365480326018fc5482e7c465a9e77824c4 (diff) | |
| download | perlweeklychallenge-club-1c59c21ec7e7a43ae44f587a7d279b90a31fbb62.tar.gz perlweeklychallenge-club-1c59c21ec7e7a43ae44f587a7d279b90a31fbb62.tar.bz2 perlweeklychallenge-club-1c59c21ec7e7a43ae44f587a7d279b90a31fbb62.zip | |
Merge pull request #3945 from Abigail/abigail/week-109
Abigail/week 109
52 files changed, 1540 insertions, 38 deletions
diff --git a/challenge-109/abigail/README.md b/challenge-109/abigail/README.md index 32dc7d1d9d..6a72e49070 100644 --- a/challenge-109/abigail/README.md +++ b/challenge-109/abigail/README.md @@ -1,70 +1,106 @@ # Solutions by Asbigail -## [Locate Memory](https://perlweeklychallenge.org/blog/perl-weekly-challenge-108/#TASK1) +## [Chowla Numbers](https://perlweeklychallenge.org/blog/perl-weekly-challenge-109/#TASK1) -Write a script to declare a variable or constant and print it's -location in the memory. +Write a script to generate first 20 Chowla Numbers, named after, +Sarvadaman D. S. Chowla, a London born Indian American mathematician. +It is defined as: + + C(n) = (sum of divisors of n) - 1 - n + +### Output + + 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 ### Solutions +* [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) +* [BASIC](basic/ch-1.bas) +* [bc](bc/ch-1.bc) +* [Befunge-93][befunge-93/ch-1.bf93] * [C](c/ch-1.c) +* [Cobol](cobol/ch-1.cb) +* [Csh](csh/ch-1.csh) +* [Erlang](erlang/ch-1.erl) +* [Forth](forth/ch-1.fs) * [Fortran](fortran/ch-1.f90) * [Go](go/ch-1.go) +* [Java](java/ch-1.java) +* [Lua](lua/ch-1.lua) +* [m4](m4/ch-1.m4) +* [Node.js](node/ch-1.js) +* [Ocaml](ocaml/ch-1.ml) * [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) +* [PHP](php/ch-1.php) +* [PostScript](postscript/ch-1.ps) * [Python](python/ch-1.py) * [R](r/ch-1.r) +* [Rexx](rexx/ch-1.rexx) * [Ruby](ruby/ch-1.rb) +* [Scheme](scheme/ch-1.scm) +* [sed](sed/ch-1.sed) +* [SQL](sql/ch-1.sql) +* [Tcl](tcl/ch-1.tcl) ### Blog -[Locate Memory](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-108-1.html) +[Chowla Numbers](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-109-1.html) + + +## [Four Squares Puzzle](https://perlweeklychallenge.org/blog/perl-weekly-challenge-109/#TASK2) -## [Bell Numbers](https://perlweeklychallenge.org/blog/perl-weekly-challenge-108/#TASK2) +You are given four squares as below with numbers named `a`, `b`, +`c`, `d`, `e`, `f`, `g`. -Write a script to display top 10 Bell Numbers. Please refer to -[wikipedia page](https://en.wikipedia.org/wiki/Bell_number) for -more informations. + (1) (3) + ╔══════════════╗ ╔══════════════╗ + ║ ║ ║ ║ + ║ a ║ ║ e ║ + ║ ║ (2) ║ ║ (4) + ║ ┌───╫──────╫───┐ ┌───╫─────────┐ + ║ │ ║ ║ │ │ ║ │ + ║ │ b ║ ║ d │ │ f ║ │ + ║ │ ║ ║ │ │ ║ │ + ║ │ ║ ║ │ │ ║ │ + ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │ + │ c │ │ g │ + │ │ │ │ + │ │ │ │ + └──────────────┘ └─────────────┘ + +Write a script to place the given unique numbers in the square box +so that sum of numbers in each box is the same. ### Example -* `B_0 = 1`, as you can only have one partition of zero element set -* `B_1 = 1`, as you can only have one partition of one element set {a}. -* `B_2 = 2`, `{a}{b}`, `{a,b}`. -* `B_3 = 5`, `{a}{b}{c}`, `{a,b}{c}`, `{a}{b,c}`, `{a,c}{b}`, `{a,b,c}`. -* `B_4 = 15`, `{a}{b}{c}{d}`, `{a,b,c,d}`, `{a,b}{c,d}`, `{a,c}{b,d}`, - `{a,d}{b,c}`, `{a,b}{c}{d}`, `{a,c}{b}{d}`, `{a,d}{b}{c}`, - `{b,c}{a}{d}`, `{b,d}{a}{c}`, `{c,d}{a}{b}`, `{a}{b,c,d}`, - `{b}{a,c,d}`, `{c}{a,b,d}`, `{d}{a,b,c}` + +~~~~ +Input: 1,2,3,4,5,6,7 + +Output: + + a = 6 + b = 4 + c = 1 + d = 5 + e = 2 + f = 3 + g = 7 + + Box 1: a + b = 6 + 4 = 10 + Box 2: b + c + d = 4 + 1 + 5 = 10 + Box 3: d + e + f = 5 + 2 + 3 = 10 + Box 4: f + g = 3 + 7 = 10 +~~~~ ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [BASIC](basic/ch-2.bas) -* [bc](bc/ch-2.bc) -* [Befunge-93][befunge-93/ch-2.bf93] * [C](c/ch-2.c) -* [Cobol](cobol/ch-2.cb) -* [Csh](csh/ch-2.csh) -* [Erlang](erlang/ch-2.erl) -* [Forth](forth/ch-2.fs) -* [Fortran](fortran/ch-2.f90) -* [Go](go/ch-2.go) -* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) -* [m4](m4/ch-2.m4) * [Node.js](node/ch-2.js) -* [Ocaml](ocaml/ch-2.ml) -* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) -* [PHP](php/ch-2.php) -* [PostScript](postscript/ch-2.ps) * [Python](python/ch-2.py) -* [R](r/ch-2.r) -* [Rexx](rexx/ch-2.rexx) * [Ruby](ruby/ch-2.rb) -* [Scheme](scheme/ch-2.scm) -* [sed](sed/ch-2.sed) -* [SQL](sql/ch-2.sql) -* [Tcl](tcl/ch-2.tcl) ### Blog -[Bell Numbers](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-108-2.html) diff --git a/challenge-109/abigail/awk/ch-1.awk b/challenge-109/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..203c7f1fec --- /dev/null +++ b/challenge-109/abigail/awk/ch-1.awk @@ -0,0 +1,37 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk [plain | compute] +# + +function divisor_sum (n, i) { + sum = 0 + for (i = 2; i <= n / 2; i ++) { + if (n % i == 0) { + sum += i + } + } + return (sum) +} + +BEGIN { + COUNT = 20 + if (!ARGV [1] || ARGV [1] != "compute") { + print "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21" + } + else { + for (i = 1; i <= COUNT; i ++) { + if (i > 1) { + printf ", "; + } + printf "%d", divisor_sum(i) + } + printf "\n"; + } + delete ARGV +} + diff --git a/challenge-109/abigail/awk/ch-2.awk b/challenge-109/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..9476ec90e8 --- /dev/null +++ b/challenge-109/abigail/awk/ch-2.awk @@ -0,0 +1,126 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + SIZE = 7; + # + # Put the fields into an array. + # + for (i = 1; i <= SIZE; i ++) { + numbers [i] = $i + } + + # + # Find all the differences between pairs of numbers + # + delete diffs + delete diff_count + for (i = 1; i <= SIZE; i ++) { + for (j = i + 1; j <= SIZE; j ++) { + diff = numbers [i] - numbers [j] + diff_count [ diff] ++ + diff_count [-diff] ++ + l1 = diff_count [ diff] + l2 = diff_count [-diff] + diffs [ diff, l1, 1] = i + diffs [ diff, l1, 2] = j + diffs [-diff, l2, 1] = j + diffs [-diff, l2, 2] = i + } + } + + # + # Iterate over the numbers, and see if there is a number + # which is equal to at least two differences. + # + # These numbers will be possibilities for d (at index d_i) + # + for (d_i = 1; d_i <= SIZE; d_i ++) { + d = numbers [d_i] + c = diff_count [d] + if (diff_count [d] >= 2) { + # + # Find two pairs whose difference is d, where none + # of the five numbers is reused. + # + for (i = 1; i <= c; i ++) { + if (diffs [d, i, 1] != d_i && diffs [d, i, 2] != d_i) { + for (j = i + 1; j <= c; j ++) { + if (diffs [d, j, 1] != d_i && + diffs [d, j, 2] != d_i && + diffs [d, i, 1] != diffs [d, j, 1] && + diffs [d, i, 1] != diffs [d, j, 2] && + diffs [d, i, 2] != diffs [d, j, 1] && + diffs [d, i, 2] != diffs [d, j, 2]) { + # + # W.l.o.g we can now assume diffs [d, i] + # contains the indices for a and c, + # and diffs [d, j] the indices for g and e. + # + a_i = diffs [d, i, 1] + c_i = diffs [d, i, 2] + g_i = diffs [d, j, 1] + e_i = diffs [d, j, 2] + + # + # Find the unused positions for b and f + # + for (b_i = 1; b_i <= SIZE; b_i ++) { + if (b_i != a_i && b_i != c_i && b_i != d_i && + b_i != e_i && b_i != g_i) { + for (f_i = 1; f_i <= SIZE; f_i ++) { + if (f_i != a_i && f_i != b_i && + f_i != c_i && f_i != d_i && + f_i != e_i && f_i != g_i) { + # + # Do we have a winner? + # + target = numbers [a_i] + \ + numbers [b_i] + if (target == numbers [b_i] + \ + numbers [c_i] + \ + numbers [d_i] && + target == numbers [d_i] + \ + numbers [e_i] + \ + numbers [f_i] && + target == numbers [f_i] + \ + numbers [g_i]) { + # + # We have a winner. Print + # it, and the reverse + # + printf "%d %d %d %d %d %d %d\n", + numbers [a_i], + numbers [b_i], + numbers [c_i], + numbers [d_i], + numbers [e_i], + numbers [f_i], + numbers [g_i] + printf "%d %d %d %d %d %d %d\n", + numbers [g_i], + numbers [f_i], + numbers [e_i], + numbers [d_i], + numbers [c_i], + numbers [b_i], + numbers [a_i] + } + } + } + } + } + } + } + } + } + } + } +} diff --git a/challenge-109/abigail/bash/ch-1.sh b/challenge-109/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..752a4f9760 --- /dev/null +++ b/challenge-109/abigail/bash/ch-1.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh [plain | compute] +# + +set -f + +COUNT=20 + +function divisor_sum () { + local n=$1 + sum=0 + local i + for ((i = 2; i <= n / 2; i ++)) + do if ((n % i == 0)) + then ((sum += i)) + fi + done +} + +if [ "X$1" != "Xcompute" ] +then echo "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21" +else for ((n = 1; n <= COUNT; n ++)) + do if ((n > 1)) + then printf ", " + fi + divisor_sum $n + printf $sum + done + echo "" +fi diff --git a/challenge-109/abigail/bash/ch-2.sh b/challenge-109/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..175ddf807d --- /dev/null +++ b/challenge-109/abigail/bash/ch-2.sh @@ -0,0 +1,83 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +SIZE=7 + +# +# Just try all possibilities, but continue as soon as we know +# this cannot lead to a solution. +# +# We use 7 variables, a_i .. g_i indicating the indices of of +# the numbers we try for a .. g. We use nested loops to iterate +# over all the possibilties. We continue with the next iteration +# of a loop if: +# +# - We pick an index which is already taken by an outer loop. +# - We have a mismatch in the sums in the squares: +# + We define a target = numbers [a_i] + numbers [b_i] +# + After picking c_i and d_i, we check whether +# numbers [b_i] + numbers [c_i] + numbers [d_i] equals target. +# + After picking e_i and f_i, we check whether +# numbers [d_i] + numbers [e_i] + numbers [f_i] equals target. +# + After picking g_i, we check whether +# numbers [f_i] + numbers [g_i] equals target. +# + If we pass all tests, we have a solution. +# + +while read -a numbers +do for ((a_i = 0; a_i < SIZE; a_i ++)) + do for ((b_i = 0; b_i < SIZE; b_i ++)) + do if ((a_i == b_i)) + then continue + fi + ((target = numbers[a_i] + numbers[b_i])) + for ((c_i = 0; c_i < SIZE; c_i ++)) + do if ((a_i == c_i || b_i == c_i)) + then continue + fi + for ((d_i = 0; d_i < SIZE; d_i ++)) + do if ((a_i == d_i || b_i == d_i || c_i == d_i || + target != numbers[b_i] + numbers[c_i] + + numbers[d_i])) + then continue + fi + for ((e_i = 0; e_i < SIZE; e_i ++)) + do if ((a_i == e_i || b_i == e_i || + c_i == e_i || d_i == e_i)) + then continue + fi + for ((f_i = 0; f_i < SIZE; f_i ++)) + do if ((a_i == f_i || b_i == f_i || + c_i == f_i || d_i == f_i || + e_i == f_i || + target != numbers[d_i] + numbers[e_i] + + numbers[f_i])) + then continue + fi + for ((g_i = 0; g_i < SIZE; g_i ++)) + do if ((a_i == g_i || b_i == g_i || + c_i == g_i || d_i == g_i || + e_i == g_i || f_i == g_i || + target != numbers[f_i] + numbers[g_i])) + then continue + fi + printf "%d %d %d %d %d %d %d\n" \ + ${numbers[$a_i]} ${numbers[$b_i]} \ + ${numbers[$c_i]} ${numbers[$d_i]} \ + ${numbers[$e_i]} ${numbers[$f_i]} \ + ${numbers[$g_i]} + done + done + done + done + done + done + done +done diff --git a/challenge-109/abigail/basic/ch-1.bas b/challenge-109/abigail/basic/ch-1.bas new file mode 100644 index 0000000000..2af64900eb --- /dev/null +++ b/challenge-109/abigail/basic/ch-1.bas @@ -0,0 +1,9 @@ +010 REM +020 REM See ../README.md +030 REM + +040 REM +050 REM Run as: basic ch-1.bas +060 REM + +100 PRINT "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21" diff --git a/challenge-109/abigail/bc/ch-1.bc b/challenge-109/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..4c739fdd23 --- /dev/null +++ b/challenge-109/abigail/bc/ch-1.bc @@ -0,0 +1,10 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc +# +"0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 +" +quit diff --git a/challenge-109/abigail/befunge-93/ch-1.bf93 b/challenge-109/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..e5f947956f --- /dev/null +++ b/challenge-109/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,2 @@ +< v,_@#:< "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21" +55 + > ^ diff --git a/challenge-109/abigail/blog.txt b/challenge-109/abigail/blog.txt new file mode 100644 index 0000000000..a9b6fb2f3e --- /dev/null +++ b/challenge-109/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-109-1.html diff --git a/challenge-109/abigail/c/ch-1.c b/challenge-109/abigail/c/ch-1.c new file mode 100644 index 0000000000..377caf63f3 --- /dev/null +++ b/challenge-109/abigail/c/ch-1.c @@ -0,0 +1,53 @@ +# 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 [plain | compute] + */ + +# define PLAIN 0 +# define COMPUTE 1 + +# define COUNT 20 + +typedef int number; /* Change if we want large numbers */ +# define fmt "%d" + +number divisor_sum (number n) { + number sum = 0; + for (number i = 2; i <= n / 2; i ++) { + if (!(n % i)) { + sum += i; + } + } + return (sum); +} + +int main (int argc, char * argv []) { + int type = PLAIN; + if (argc > 1) { + if (strncmp (argv [1], "compute", 8) == 0) { + type = COMPUTE; + } + } + + if (type == PLAIN) { + printf ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, "); + printf ("0, 15, 0, 9, 8, 14, 0, 20, 0, 21\n"); + } + if (type == COMPUTE) { + for (number i = 1; i <= COUNT; i ++) { + if (i != 1) { + printf (", "); + } + printf (fmt, divisor_sum (i)); + } + printf ("\n"); + } + exit (0); +} diff --git a/challenge-109/abigail/c/ch-2.c b/challenge-109/abigail/c/ch-2.c new file mode 100644 index 0000000000..19d95ad9f5 --- /dev/null +++ b/challenge-109/abigail/c/ch-2.c @@ -0,0 +1,87 @@ +# 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 + */ + +# define SIZE 7 +typedef int number; + +int main (void) { + number * numbers; + char * line = NULL; + size_t len = 0; + + if ((numbers = (number *) malloc (SIZE * sizeof (number))) == NULL) { + perror ("Malloc numbers failed"); + exit (1); + } + + while (getline (&line, &len, stdin) != -1) { + if (sscanf (line, "%d %d %d %d %d %d %d", &numbers [0], + &numbers [1], &numbers [2], &numbers [3], + &numbers [4], &numbers [5], &numbers [6]) != SIZE) { + continue; + } + + for (size_t a_i = 0; a_i < SIZE; a_i ++) { + for (size_t b_i = 0; b_i < SIZE; b_i ++) { + if (a_i == b_i) { + continue; + } + number target = numbers [a_i] + numbers [b_i]; + for (size_t c_i = 0; c_i < SIZE; c_i ++) { + if (a_i == c_i || b_i == c_i) { + continue; + } + for (size_t d_i = 0; d_i < SIZE; d_i ++) { + if (a_i == d_i || b_i == d_i || c_i == d_i || + target != numbers [b_i] + numbers [c_i] + + numbers [d_i]) { + |
