From 75c953a1d50cd707e2afe4dd46888056a71eb501 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 19 Apr 2021 13:49:21 +0200 Subject: README.md for week 109 --- challenge-109/abigail/README.md | 105 +++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 54 deletions(-) diff --git a/challenge-109/abigail/README.md b/challenge-109/abigail/README.md index 32dc7d1d9d..860949d790 100644 --- a/challenge-109/abigail/README.md +++ b/challenge-109/abigail/README.md @@ -1,70 +1,67 @@ # 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 -* [C](c/ch-1.c) -* [Fortran](fortran/ch-1.f90) -* [Go](go/ch-1.go) -* [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) ### Blog -[Locate Memory](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-108-1.html) -## [Bell Numbers](https://perlweeklychallenge.org/blog/perl-weekly-challenge-108/#TASK2) +## [Four Squares Puzzle](https://perlweeklychallenge.org/blog/perl-weekly-challenge-109/#TASK2) + +You are given four squares as below with numbers named `a`, `b`, +`c`, `d`, `e`, `f`, `g`. + + (1) (3) + ╔══════════════╗ ╔══════════════╗ + ║ ║ ║ ║ + ║ a ║ ║ e ║ + ║ ║ (2) ║ ║ (4) + ║ ┌───╫──────╫───┐ ┌───╫─────────┐ + ║ │ ║ ║ │ │ ║ │ + ║ │ b ║ ║ d │ │ f ║ │ + ║ │ ║ ║ │ │ ║ │ + ║ │ ║ ║ │ │ ║ │ + ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │ + │ c │ │ 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. +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) -- cgit From 610ff9605ec7b308051a8f04f9b21d6f4060a8cd Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 19 Apr 2021 20:16:47 +0200 Subject: Solutions in 29 languages for week 109, part 1. --- challenge-109/abigail/README.md | 29 ++++++++++++++++ challenge-109/abigail/awk/ch-1.awk | 37 +++++++++++++++++++++ challenge-109/abigail/bash/ch-1.sh | 35 ++++++++++++++++++++ challenge-109/abigail/basic/ch-1.bas | 9 +++++ challenge-109/abigail/bc/ch-1.bc | 10 ++++++ challenge-109/abigail/befunge-93/ch-1.bf93 | 2 ++ challenge-109/abigail/c/ch-1.c | 53 ++++++++++++++++++++++++++++++ challenge-109/abigail/cobol/ch-1.cb | 14 ++++++++ challenge-109/abigail/csh/ch-1.csh | 11 +++++++ challenge-109/abigail/erlang/ch-1.erl | 15 +++++++++ challenge-109/abigail/forth/ch-1.fs | 5 +++ challenge-109/abigail/fortran/ch-1.f90 | 12 +++++++ challenge-109/abigail/go/ch-1.go | 15 +++++++++ challenge-109/abigail/java/ch-1.java | 13 ++++++++ challenge-109/abigail/lua/ch-1.lua | 43 ++++++++++++++++++++++++ challenge-109/abigail/m4/ch-1.m4 | 1 + challenge-109/abigail/node/ch-1.js | 45 +++++++++++++++++++++++++ challenge-109/abigail/ocaml/ch-1.ml | 9 +++++ challenge-109/abigail/pascal/ch-1.p | 13 ++++++++ challenge-109/abigail/perl/ch-1.pl | 51 ++++++++++++++++++++++++++++ challenge-109/abigail/php/ch-1.php | 11 +++++++ challenge-109/abigail/postscript/ch-1.ps | 10 ++++++ challenge-109/abigail/python/ch-1.py | 39 ++++++++++++++++++++++ challenge-109/abigail/r/ch-1.r | 9 +++++ challenge-109/abigail/rexx/ch-1.rexx | 9 +++++ challenge-109/abigail/ruby/ch-1.rb | 44 +++++++++++++++++++++++++ challenge-109/abigail/scheme/ch-1.scm | 9 +++++ challenge-109/abigail/sed/ch-1.sed | 12 +++++++ challenge-109/abigail/sql/ch-1.sql | 9 +++++ challenge-109/abigail/t/ctest.ini | 35 ++++++++++++++++++++ challenge-109/abigail/t/input-1-1 | 1 + challenge-109/abigail/t/input-1-2 | 0 challenge-109/abigail/t/input-1-3 | 0 challenge-109/abigail/t/input-1-4 | 0 challenge-109/abigail/t/output-1-1.exp | 1 + challenge-109/abigail/t/output-1-2.exp | 1 + challenge-109/abigail/t/output-1-3.exp | 1 + challenge-109/abigail/t/output-1-4.exp | 1 + challenge-109/abigail/tcl/ch-1.tcl | 9 +++++ 39 files changed, 623 insertions(+) create mode 100644 challenge-109/abigail/awk/ch-1.awk create mode 100644 challenge-109/abigail/bash/ch-1.sh create mode 100644 challenge-109/abigail/basic/ch-1.bas create mode 100644 challenge-109/abigail/bc/ch-1.bc create mode 100644 challenge-109/abigail/befunge-93/ch-1.bf93 create mode 100644 challenge-109/abigail/c/ch-1.c create mode 100644 challenge-109/abigail/cobol/ch-1.cb create mode 100644 challenge-109/abigail/csh/ch-1.csh create mode 100644 challenge-109/abigail/erlang/ch-1.erl create mode 100644 challenge-109/abigail/forth/ch-1.fs create mode 100644 challenge-109/abigail/fortran/ch-1.f90 create mode 100644 challenge-109/abigail/go/ch-1.go create mode 100644 challenge-109/abigail/java/ch-1.java create mode 100644 challenge-109/abigail/lua/ch-1.lua create mode 100644 challenge-109/abigail/m4/ch-1.m4 create mode 100644 challenge-109/abigail/node/ch-1.js create mode 100644 challenge-109/abigail/ocaml/ch-1.ml create mode 100644 challenge-109/abigail/pascal/ch-1.p create mode 100644 challenge-109/abigail/perl/ch-1.pl create mode 100644 challenge-109/abigail/php/ch-1.php create mode 100644 challenge-109/abigail/postscript/ch-1.ps create mode 100644 challenge-109/abigail/python/ch-1.py create mode 100644 challenge-109/abigail/r/ch-1.r create mode 100644 challenge-109/abigail/rexx/ch-1.rexx create mode 100644 challenge-109/abigail/ruby/ch-1.rb create mode 100644 challenge-109/abigail/scheme/ch-1.scm create mode 100644 challenge-109/abigail/sed/ch-1.sed create mode 100644 challenge-109/abigail/sql/ch-1.sql create mode 100644 challenge-109/abigail/t/ctest.ini create mode 100644 challenge-109/abigail/t/input-1-1 create mode 100644 challenge-109/abigail/t/input-1-2 create mode 100644 challenge-109/abigail/t/input-1-3 create mode 100644 challenge-109/abigail/t/input-1-4 create mode 100644 challenge-109/abigail/t/output-1-1.exp create mode 100644 challenge-109/abigail/t/output-1-2.exp create mode 100644 challenge-109/abigail/t/output-1-3.exp create mode 100644 challenge-109/abigail/t/output-1-4.exp create mode 100644 challenge-109/abigail/tcl/ch-1.tcl diff --git a/challenge-109/abigail/README.md b/challenge-109/abigail/README.md index 860949d790..57a25f7869 100644 --- a/challenge-109/abigail/README.md +++ b/challenge-109/abigail/README.md @@ -12,6 +12,35 @@ It is defined as: 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 diff --git a/challenge-109/abigail/awk/ch-1.awk b/challenge-109/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..6b889ae3a0 --- /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; 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/bash/ch-1.sh b/challenge-109/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..d4eec1f2d9 --- /dev/null +++ b/challenge-109/abigail/bash/ch-1.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh [plain | compute] +# + +set -f + +COUNT=20 + +function divisor_sum () { + sum=0 + local i + for ((i = 2; i < n; 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/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/c/ch-1.c b/challenge-109/abigail/c/ch-1.c new file mode 100644 index 0000000000..e6716c148a --- /dev/null +++ b/challenge-109/abigail/c/ch-1.c @@ -0,0 +1,53 @@ +# include +# include +# include + +/* + * 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; 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/cobol/ch-1.cb b/challenge-109/abigail/cobol/ch-1.cb new file mode 100644 index 0000000000..06a7dcece4 --- /dev/null +++ b/challenge-109/abigail/cobol/ch-1.cb @@ -0,0 +1,14 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. XXX. + +*> +*> See ../README.md +*> + +*> +*> Run as: cobc -xF -o ch-1.o ch-1.cb; ./ch-1.o +*> + +PROCEDURE DIVISION. + DISPLAY "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21". + STOP RUN. diff --git a/challenge-109/abigail/csh/ch-1.csh b/challenge-109/abigail/csh/ch-1.csh new file mode 100644 index 0000000000..9a612ffab8 --- /dev/null +++ b/challenge-109/abigail/csh/ch-1.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See ../README.md +# + +# +# Run as: csh ch-1.csh +# + +echo "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/erlang/ch-1.erl b/challenge-109/abigail/erlang/ch-1.erl new file mode 100644 index 0000000000..c0b458def9 --- /dev/null +++ b/challenge-109/abigail/erlang/ch-1.erl @@ -0,0 +1,15 @@ +% +% See ../README.md +% + +% +% Run as: ln ch-1.erl ch1.erl +% erl -compile ch1 +% erl -noshell -s ch1 main -s init stop +% + +-module (ch1). +-export ([main/0]). + +main () -> + io:fwrite ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21\n"). diff --git a/challenge-109/abigail/forth/ch-1.fs b/challenge-109/abigail/forth/ch-1.fs new file mode 100644 index 0000000000..8a88bf0b28 --- /dev/null +++ b/challenge-109/abigail/forth/ch-1.fs @@ -0,0 +1,5 @@ +\ +\ See ../README.md +\ + +.( 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/fortran/ch-1.f90 b/challenge-109/abigail/fortran/ch-1.f90 new file mode 100644 index 0000000000..be6055f839 --- /dev/null +++ b/challenge-109/abigail/fortran/ch-1.f90 @@ -0,0 +1,12 @@ +! +! See ../README.md +! + +! +! Run as: gfortran -o ch-1.o ch-1.f90; ./ch-1.o +! + +program ch1 + implicit none + write (*, *) "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21" +end diff --git a/challenge-109/abigail/go/ch-1.go b/challenge-109/abigail/go/ch-1.go new file mode 100644 index 0000000000..78d9ca1148 --- /dev/null +++ b/challenge-109/abigail/go/ch-1.go @@ -0,0 +1,15 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import "fmt" + +func main () { + fmt . Print ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21\n") +} diff --git a/challenge-109/abigail/java/ch-1.java b/challenge-109/abigail/java/ch-1.java new file mode 100644 index 0000000000..b50b73f0d2 --- /dev/null +++ b/challenge-109/abigail/java/ch-1.java @@ -0,0 +1,13 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1; java ch1 +// + +public class ch1 { + public static void main (String [] args) { + System . out . print ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21\n"); + } +} diff --git a/challenge-109/abigail/lua/ch-1.lua b/challenge-109/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..5eb2d477f9 --- /dev/null +++ b/challenge-109/abigail/lua/ch-1.lua @@ -0,0 +1,43 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua [plain | compute] +-- + +local PLAIN = 0 +local COMPUTE = 1 + +local COUNT = 20 + +function divisor_sum (n) + local sum = 0 + for i = 2, n - 2 do + if n % i == 0 + then sum = sum + i + end + end + return (sum) +end + +local type = PLAIN +if #arg >= 1 and arg [1] == "compute" +then type = COMPUTE +end + +if type == PLAIN +then print ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21") +end + +if type == COMPUTE +then for n = 1, COUNT do + if n > 1 + then io . write (", ") + end + io . write (divisor_sum (n)) + end + io . write ("\n") +end diff --git a/challenge-109/abigail/m4/ch-1.m4 b/challenge-109/abigail/m4/ch-1.m4 new file mode 100644 index 0000000000..9299e87f97 --- /dev/null +++ b/challenge-109/abigail/m4/ch-1.m4 @@ -0,0 +1 @@ +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/node/ch-1.js b/challenge-109/abigail/node/ch-1.js new file mode 100644 index 0000000000..d7cff7b0f5 --- /dev/null +++ b/challenge-109/abigail/node/ch-1.js @@ -0,0 +1,45 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js [plain | compute] +// + +let PLAIN = 0 +let COMPUTE = 1 + +let COUNT = 20 + +function divisor_sum (n) { + let sum = 0 + for (let i = 2; i < n; i ++) { + if (n % i == 0) { + sum += i + } + } + return (sum) +} + +let type = PLAIN +if (process . argv . length > 2 && + process . argv [2] == "compute") { + type = COMPUTE +} + +if (type == PLAIN) { + console . log ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, " + + "0, 15, 0, 9, 8, 14, 0, 20, 0, 21") +} + +if (type == COMPUTE) { + for (let i = 1; i <= COUNT; i ++) { + if (i > 1) { + process . stdout . write (", ") + } + process . stdout . write (divisor_sum (i) . toString ()) + } + process . stdout . write ("\n") +} diff --git a/challenge-109/abigail/ocaml/ch-1.ml b/challenge-109/abigail/ocaml/ch-1.ml new file mode 100644 index 0000000000..3311ee44fa --- /dev/null +++ b/challenge-109/abigail/ocaml/ch-1.ml @@ -0,0 +1,9 @@ +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: ocaml ch-1.ml *) +(* *) + +print_endline "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/pascal/ch-1.p b/challenge-109/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..c5d97f86bb --- /dev/null +++ b/challenge-109/abigail/pascal/ch-1.p @@ -0,0 +1,13 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out *) +(* *) + +begin + writeln ('0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21') +end. diff --git a/challenge-109/abigail/perl/ch-1.pl b/challenge-109/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..64f21d5d24 --- /dev/null +++ b/challenge-109/abigail/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/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 [plain|fetch|compute] +# + +my $COUNT = 20; + +my $TYPE_PLAIN = 0; +my $TYPE_FETCH = 1; +my $TYPE_COMPUTE = 2; + +my $type = $TYPE_PLAIN; # Default + $type = $TYPE_FETCH if @ARGV && $ARGV [0] eq "fetch"; + $type = $TYPE_COMPUTE if @ARGV && $ARGV [0] eq "compute"; + + + +if ($type == $TYPE_PLAIN) { + say "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21"; +} +elsif ($type == $TYPE_FETCH) { + use OEIS; + $, = ", "; + say oeis (48050 => $COUNT); # We want the first numbers from + # sequence A048050. +} +elsif ($type == $TYPE_COMPUTE) { + use List::Util qw [sum0]; + $, = ", "; + # + # Sum the divisors; a divisor has mod 0. This is very inefficient + # even for smallish integers, but since we're asked to only + # calculate the first 20 numbers, we don't have to care about + # efficiency. + # + say map {my $n = $_; sum0 grep {!($n % $_)} 2 .. $_ - 1} 1 .. $COUNT; +} diff --git a/challenge-109/abigail/php/ch-1.php b/challenge-109/abigail/php/ch-1.php new file mode 100644 index 0000000000..0516741cc7 --- /dev/null +++ b/challenge-109/abigail/php/ch-1.php @@ -0,0 +1,11 @@ + diff --git a/challenge-109/abigail/postscript/ch-1.ps b/challenge-109/abigail/postscript/ch-1.ps new file mode 100644 index 0000000000..2ea67f6701 --- /dev/null +++ b/challenge-109/abigail/postscript/ch-1.ps @@ -0,0 +1,10 @@ +%!PS +% +% See ../README.md +% + +% +% Run as: ps2ascii ch-1.ps +% + +(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/python/ch-1.py b/challenge-109/abigail/python/ch-1.py new file mode 100644 index 0000000000..6600bda6d5 --- /dev/null +++ b/challenge-109/abigail/python/ch-1.py @@ -0,0 +1,39 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py [plain | compute] +# + +import sys + +COUNT = 20 +PLAIN = 0 +COMPUTE = 1 + +def divisor_sum (n): + sum = 0 + for i in range (2, n): + if n % i == 0: + sum = sum + i + return (sum) + +type = PLAIN + +if len (sys . argv) > 1 and sys . argv [1] == "compute": + type = COMPUTE + + +if type == PLAIN: + print ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21") + + +if type == COMPUTE: + for n in range (1, COUNT + 1): + if n > 1: + print (", ", end = '') + print (divisor_sum (n), end = '') + print ("") diff --git a/challenge-109/abigail/r/ch-1.r b/challenge-109/abigail/r/ch-1.r new file mode 100644 index 0000000000..1c6165807d --- /dev/null +++ b/challenge-109/abigail/r/ch-1.r @@ -0,0 +1,9 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r +# + +cat ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21\n") diff --git a/challenge-109/abigail/rexx/ch-1.rexx b/challenge-109/abigail/rexx/ch-1.rexx new file mode 100644 index 0000000000..3085ab0224 --- /dev/null +++ b/challenge-109/abigail/rexx/ch-1.rexx @@ -0,0 +1,9 @@ +/* + * See ../README.md + */ + +/* + * Run as: rexx ch-1.rexx + */ + +say "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/ruby/ch-1.rb b/challenge-109/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..95a38a69cb --- /dev/null +++ b/challenge-109/abigail/ruby/ch-1.rb @@ -0,0 +1,44 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb [plain | compute] +# + +PLAIN = 0 +COMPUTE = 1 + +COUNT = 20 + +def divisor_sum (n) + sum = 0 + for i in 2 .. n - 1 + if n % i == 0 + then sum += i + end + end + return sum +end + +type = PLAIN + +if ARGV . length > 0 && ARGV[0] == "compute" +then type = COMPUTE +end + +if type == PLAIN +then puts ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21"); +end + +if type == COMPUTE +then for n in 1 .. COUNT + if n > 1 + then print (", ") + end + print divisor_sum (n) + end + puts ("") +end diff --git a/challenge-109/abigail/scheme/ch-1.scm b/challenge-109/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..bc43f3c502 --- /dev/null +++ b/challenge-109/abigail/scheme/ch-1.scm @@ -0,0 +1,9 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + +(display "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21\n") diff --git a/challenge-109/abigail/sed/ch-1.sed b/challenge-109/abigail/sed/ch-1.sed new file mode 100644 index 0000000000..1c93d3f21c --- /dev/null +++ b/challenge-109/abigail/sed/ch-1.sed @@ -0,0 +1,12 @@ +# +# See ../README.md +# + +# +# Run as: sed -f ch-1.sed +# +# For each line in the input file, we write the first three +# self-describing numbers. +# + +s/.*/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/sql/ch-1.sql b/challenge-109/abigail/sql/ch-1.sql new file mode 100644 index 0000000000..014860fcd5 --- /dev/null +++ b/challenge-109/abigail/sql/ch-1.sql @@ -0,0 +1,9 @@ +-- +-- See ../README.md +-- + +-- +-- Run as: sqlite3 < ch-1.sql +-- + +SELECT "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/t/ctest.ini b/challenge-109/abigail/t/ctest.ini new file mode 100644 index 0000000000..e2f88e41ce --- /dev/null +++ b/challenge-109/abigail/t/ctest.ini @@ -0,0 +1,35 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Fixed output +1-2 = Fixed output/plain +1-3 = Fixed output/fetch +1-4 = Fixed output/compute + + +[1-1] +no_input = 1 + +[1-1/sed] +no_input = 0 + +[1-1/awk,bash,c,lua,node,perl,python,ruby] +skip = "Not for this language" + +[1-2,1-3,1-4] +skip = "Not for this language" + +[1-2/awk,bash,c,lua,node,perl,python,ruby] +skip = 0 +args = plain + +[1-3/perl] +skip = 0 +args = fetch + +[1-4/awk,bash,c,lua,node,perl,python,ruby] +skip = 0 +args = compute diff --git a/challenge-109/abigail/t/input-1-1 b/challenge-109/abigail/t/input-1-1 new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/challenge-109/abigail/t/input-1-1 @@ -0,0 +1 @@ + diff --git a/challenge-109/abigail/t/input-1-2 b/challenge-109/abigail/t/input-1-2 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/abigail/t/input-1-3 b/challenge-109/abigail/t/input-1-3 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/abigail/t/input-1-4 b/challenge-109/abigail/t/input-1-4 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/abigail/t/output-1-1.exp b/challenge-109/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..9299e87f97 --- /dev/null +++ b/challenge-109/abigail/t/output-1-1.exp @@ -0,0 +1 @@ +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/t/output-1-2.exp b/challenge-109/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..9299e87f97 --- /dev/null +++ b/challenge-109/abigail/t/output-1-2.exp @@ -0,0 +1 @@ +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/t/output-1-3.exp b/challenge-109/abigail/t/output-1-3.exp new file mode 100644 index 0000000000..9299e87f97 --- /dev/null +++ b/challenge-109/abigail/t/output-1-3.exp @@ -0,0 +1 @@ +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/t/output-1-4.exp b/challenge-109/abigail/t/output-1-4.exp new file mode 100644 index 0000000000..9299e87f97 --- /dev/null +++ b/challenge-109/abigail/t/output-1-4.exp @@ -0,0 +1 @@ +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/tcl/ch-1.tcl b/challenge-109/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..7ab10d4661 --- /dev/null +++ b/challenge-109/abigail/tcl/ch-1.tcl @@ -0,0 +1,9 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl +# + +puts "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21" -- cgit From 5be6a97a3cf0ff10e023171729a50a386c24622b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 20 Apr 2021 18:24:30 +0200 Subject: Perl solution for week 109, part 2 --- challenge-109/abigail/README.md | 1 + challenge-109/abigail/perl/ch-2.pl | 183 +++++++++++++++++++++++++++++++++ challenge-109/abigail/t/ctest.ini | 6 ++ challenge-109/abigail/t/input-2-1 | 1 + challenge-109/abigail/t/input-2-2 | 1 + challenge-109/abigail/t/output-2-1.exp | 8 ++ challenge-109/abigail/t/output-2-2.exp | 2 + 7 files changed, 202 insertions(+) create mode 100644 challenge-109/abigail/perl/ch-2.pl create mode 100644 challenge-109/abigail/t/input-2-1 create mode 100644 challenge-109/abigail/t/input-2-2 create mode 100644 challenge-109/abigail/t/output-2-1.exp create mode 100644 challenge-109/abigail/t/output-2-2.exp diff --git a/challenge-109/abigail/README.md b/challenge-109/abigail/README.md index 57a25f7869..1f7b55f195 100644 --- a/challenge-109/abigail/README.md +++ b/challenge-109/abigail/README.md @@ -91,6 +91,7 @@ Output: ~~~~ ### Solutions +* [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-109/abigail/perl/ch-2.pl b/challenge-109/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..ddbbe4fb38 --- /dev/null +++ b/challenge-109/abigail/perl/ch-2.pl @@ -0,0 +1,183 @@ +#!/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 +# + +# +# First note that due to symmetry, there cannot be a unique solution [1]. +# If +# +# (a, b, c, d, e, f, g) = (x_1, x_2, x_3, x_4, x_5, x_6, x_7) +# +# is a solution, then +# +# (a, b, c, d, e, f, g) = (x_7, x_6, x_5, x_4, x_3, x_2, x_1) +# +# must be a solution as well. +# +# For instance, the given example has 8 solutions, 4 of which are +# mirror images. +# +# [1] This assumes no duplicate numbers in the input. Clearly, a +# solution where a == g, b == f, and c == e doesn't give a +# different solution when mirrorred. (For instance, if all +# numbers are 0, there is only one solution). + +# +# +# It's easy to brute force this. There are a mere 5040 permutations +# of the seven numbers; it would be hard to find an environment +# where it takes more than a few milliseconds to try all permutations. +# +# We can improve the brute force a little, by determining early we're +# on a wrong path. For instance, once we have picked an a, b, c, and d, +# we can check whether a + b == b + c + d. If not, we don't have to +# continue trying values for e, f, and g. +# +# However, that's not the way we go. We will first do some analysis. +# Let the sum of each box be N. Then we have: +# +# N = a + b (1) +# N = b + c + d (2) +# N = d + e + f (3) +# N = f + g (4) +# +# For (1) and (2), we get: +# +# a + b = b + c + d => +# a = c + d => +# a - c = d +# +# In the same way, (3) and (4), we get: +# +# g + f = f + e + d => +# g = e + d => +# g - e = d +# +# This leads to the following algorithm: +# +# - Calculate the differences between all pairs (7 * 6 == 42 pairs) +# - Find all numbers n from the input array for which there are at +# least two pairs giving this difference, under the condition n +# is not part of such a pair. (Note that if the input contains +# two or more of the same number, for this purpose, we treat those +# numbers to be different). These numbers will be our candidate for d. +# - Of the list of differences equalling d, consider each pair. +# Eliminate pairs where the same number appears in each. The first +# difference gives candidates for a and c; the second gives candidates +# for g and e. (Swapping them leaves to a symmetric solution). +# - We now have candidates for a, c, d, e, and g. This leaves two +# numbers for b and c. +# - Try both, and check whether a + b == b + c + d == d + e + f == f + g. +# +# For the given example, this means we only try 32 permutations, +# giving us 4 different solutions (the other 4 can be found by +# reversing the numbers). +# +# We are not making any assumptions on the sign of the input numbers; +# our algorithm works fine if the input contains negative numbers. +# +# We will also print all solutions (including the symmetric ones) +# + +while (<>) { + my @numbers = split; + + # + # Find all differences + # + my %differences; + for (my $x = 0; $x < @numbers; $x ++) { + for (my $y = $x + 1; $y < @numbers; $y ++) { + my $diff = $numbers [$x] - $numbers [$y]; + push @{$differences { $diff}} => [$x, $y]; + push @{$differences {-$diff}} => [$y, $x]; + } + } + + # + # Now, iterate over the numbers, and see if there + # is a number matching two differences, with all + # indices being different. + # + for (my $d_i = 0; $d_i < @numbers; $d_i ++) { + my $d = $numbers [$d_i]; + # + # Find the pairs whose difference is $d. If we don't have + # at least two pairs where neither number is $d, this + # cannot be a solution. + # + my @diffs = grep {$$_ [0] != $d_i && $$_ [1] != $d_i} + @{$differences {$d} || []}; + + next unless @diffs >= 2; + + # + # Now, find two pairs where all indices are different. + # + for (my $x = 0; $x < @diffs; $x ++) { + for (my $y = $x + 1; $y < @diffs; $y ++) { + next if $diffs [$x] [0] == $diffs [$y] [0] || + $diffs [$x] [0] == $diffs [$y] [1] || + $diffs [$x] [1] == $diffs [$y] [0] || + $diffs [$x] [1] == $diffs [$y] [1]; + + # + # W.l.o.g. we can now assume $diffs [$x] are + # the indices for $a and $c, and $diffs [$y] + # are the indices for $g and $e. + # + my ($a_i, $c_i) = @{$diffs [$x]}; + my ($g_i, $e_i) = @{$diffs [$y]}; + + # + # Find the unused indices + # + my %indices = map {$_ => 1} keys @numbers; + delete $indices {$_} for $a_i, $c_i, $d_i, $e_i, $g_i; + + # + # This leaves two indices for $b and $e. + # Try them both. + # + my $left = [keys %indices]; + foreach my $try ($left, [reverse @$left]) { + my ($b_i, $f_i) = @$try; + + # + # Do we have a winner? + # + next unless $numbers [$a_i] + $numbers [$b_i] == + $numbers [$b_i] + $numbers [$c_i] + $numbers [$d_i] == + $numbers [$d_i] + $numbers [$e_i] + $numbers [$f_i] == + $numbers [$f_i] + $numbers [$g_i]; + + # + # Print result, and the reverse, so we get all + # possible solutions. + # + my @solution = + @numbers [$a_i, $b_i, $c_i, $d_i, $e_i, $f_i, $g_i]; + + local $, = " "; + say @solution; + say reverse @solution; + } + } + } + } +} diff --git a/challenge-109/abigail/t/ctest.ini b/challenge-109/abigail/t/ctest.ini index e2f88e41ce..afa82182bd 100644 --- a/challenge-109/abigail/t/ctest.ini +++ b/challenge-109/abigail/t/ctest.ini @@ -8,6 +8,8 @@ 1-2 = Fixed output/plain 1-3 = Fixed output/fetch 1-4 = Fixed output/compute +2-1 = Given example +2-2 = With a negative number [1-1] @@ -33,3 +35,7 @@ args = fetch [1-4/awk,bash,c,lua,node,perl,python,ruby] skip = 0 args = compute + + +[2-1,2-2] +sort = 1 diff --git a/challenge-109/abigail/t/input-2-1 b/challenge-109/abigail/t/input-2-1 new file mode 100644 index 0000000000..b4457aae66 --- /dev/null +++ b/challenge-109/abigail/t/input-2-1 @@ -0,0 +1 @@ +1 2 3 4 5 6 7 diff --git a/challenge-109/abigail/t/input-2-2 b/challenge-109/abigail/t/input-2-2 new file mode 100644 index 0000000000..335e2405f0 --- /dev/null +++ b/challenge-109/abigail/t/input-2-2 @@ -0,0 +1 @@ +1 -2 3 4 5 6 7 diff --git a/challenge-109/abigail/t/output-2-1.exp b/challenge-109/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..83687f2b3e --- /dev/null +++ b/challenge-109/abigail/t/output-2-1.exp @@ -0,0 +1,8 @@ +3 7 2 1 5 4 6 +4 5 3 1 6 2 7 +4 7 1 3 2 6 5 +5 6 2 3 1 7 4 +6 4 1 5 2 3 7 +6 4 5 1 2 7 3 +7 2 6 1 3 5 4 +7 3 2 5 1 4 6 diff --git a/challenge-109/abigail/t/output-2-2.exp b/challenge-109/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..7a67a5be9d --- /dev/null +++ b/challenge-109/abigail/t/output-2-2.exp @@ -0,0 +1,2 @@ +3 7 -2 5 1 4 6 +6 4 1 5 -2 7 3 -- cgit From 490b2215c7eaeb037b7548564306e2081784b3e4 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 20 Apr 2021 20:43:18 +0200 Subject: Use function argument --- challenge-109/abigail/bash/ch-1.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-109/abigail/bash/ch-1.sh b/challenge-109/abigail/bash/ch-1.sh index d4eec1f2d9..de09aa6414 100644 --- a/challenge-109/abigail/bash/ch-1.sh +++ b/challenge-109/abigail/bash/ch-1.sh @@ -13,6 +13,7 @@ set -f COUNT=20 function divisor_sum () { + local n=$1 sum=0 local i for ((i = 2; i < n; i ++)) -- cgit From 7b4fbbe6505b63d2faa62ad34939b6e830ea692b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 20 Apr 2021 20:52:57 +0200 Subject: When looping over possible divisors, stop at n / 2. --- challenge-109/abigail/awk/ch-1.awk | 2 +- challenge-109/abigail/bash/ch-1.sh | 2 +- challenge-109/abigail/c/ch-1.c | 2 +- challenge-109/abigail/lua/ch-1.lua | 2 +- challenge-109/abigail/node/ch-1.js | 2 +- challenge-109/abigail/perl/ch-1.pl | 2 +- challenge-109/abigail/python/ch-1.py | 2 +- challenge-109/abigail/ruby/ch-1.rb | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/challenge-109/abigail/awk/ch-1.awk b/challenge-109/abigail/awk/ch-1.awk index 6b889ae3a0..203c7f1fec 100644 --- a/challenge-109/abigail/awk/ch-1.awk +++ b/challenge-109/abigail/awk/ch-1.awk @@ -10,7 +10,7 @@ function divisor_sum (n, i) { sum = 0 - for (i = 2; i < n; i ++) { + for (i = 2; i <= n / 2; i ++) { if (n % i == 0) { sum += i } diff --git a/challenge-109/abigail/bash/ch-1.sh b/challenge-109/abigail/bash/ch-1.sh index de09aa6414..752a4f9760 100644 --- a/challenge-109/abigail/bash/ch-1.sh +++ b/challenge-109/abigail/bash/ch-1.sh @@ -16,7 +16,7 @@ function divisor_sum () { local n=$1 sum=0 local i - for ((i = 2; i < n; i ++)) + for ((i = 2; i <= n / 2; i ++)) do if ((n % i == 0)) then ((sum += i)) fi diff --git a/challenge-109/abigail/c/ch-1.c b/challenge-109/abigail/c/ch-1.c index e6716c148a..377caf63f3 100644 --- a/challenge-109/abigail/c/ch-1.c +++ b/challenge-109/abigail/c/ch-1.c @@ -20,7 +20,7 @@ typedef int number; /* Change if we want large numbers */ number divisor_sum (number n) { number sum = 0; - for (number i = 2; i < n; i ++) { + for (number i = 2; i <= n / 2; i ++) { if (!(n % i)) { sum += i; } diff --git a/challenge-109/abigail/lua/ch-1.lua b/challenge-109/abigail/lua/ch-1.lua index 5eb2d477f9..74c3b7d914 100644 --- a/challenge-109/abigail/lua/ch-1.lua +++ b/challenge-109/abigail/lua/ch-1.lua @@ -15,7 +15,7 @@ local COUNT = 20 function divisor_sum (n) local sum = 0 - for i = 2, n - 2 do + for i = 2, n / 2 do if n % i == 0 then sum = sum + i end diff --git a/challenge-109/abigail/node/ch-1.js b/challenge-109/abigail/node/ch-1.js index d7cff7b0f5..0f75cf4962 100644 --- a/challenge-109/abigail/node/ch-1.js +++ b/challenge-109/abigail/node/ch-1.js @@ -15,7 +15,7 @@ let COUNT = 20 function divisor_sum (n) { let sum = 0 - for (let i = 2; i < n; i ++) { + for (let i = 2; i <= Math . floor (n / 2); i ++) { if (n % i == 0) { sum += i } diff --git a/challenge-109/abigail/perl/ch-1.pl b/challenge-109/abigail/perl/ch-1.pl index 64f21d5d24..967a0344de 100644 --- a/challenge-109/abigail/perl/ch-1.pl +++ b/challenge-109/abigail/perl/ch-1.pl @@ -47,5 +47,5 @@ elsif ($type == $TYPE_COMPUTE) { # calculate the first 20 numbers, we don't have to care about # efficiency. # - say map {my $n = $_; sum0 grep {!($n % $_)} 2 .. $_ - 1} 1 .. $COUNT; + say map {my $n = $_; sum0 grep {!($n % $_)} 2 .. $_ / 2} 1 .. $COUNT; } diff --git a/challenge-109/abigail/python/ch-1.py b/challenge-109/abigail/python/ch-1.py index 6600bda6d5..f9e51a0897 100644 --- a/challenge-109/abigail/python/ch-1.py +++ b/challenge-109/abigail/python/ch-1.py @@ -16,7 +16,7 @@ COMPUTE = 1 def divisor_sum (n): sum = 0 - for i in range (2, n): + for i in range (2, n / 2 + 1): if n % i == 0: sum = sum + i return (sum) diff --git a/challenge-109/abigail/ruby/ch-1.rb b/challenge-109/abigail/ruby/ch-1.rb index 95a38a69cb..0c73c368dd 100644 --- a/challenge-109/abigail/ruby/ch-1.rb +++ b/challenge-109/abigail/ruby/ch-1.rb @@ -15,7 +15,7 @@ COUNT = 20 def divisor_sum (n) sum = 0 - for i in 2 .. n - 1 + for i in 2 .. n / 2 if n % i == 0 then sum += i end -- cgit From fff9e4703ed20a60640c4ef24219499eb532ecb3 Mon Sep 17 00:00:00 2001 From: Lance Wicks Date: Wed, 21 Apr 2021 20:21:20 +0100 Subject: Perl implementation for 108, thanks Mohammad! --- challenge-109/lance-wicks/perl/ch-1.pl | 12 ++++++++++++ challenge-109/lance-wicks/perl/lib/Chowla.pm | 26 ++++++++++++++++++++++++++ challenge-109/lance-wicks/perl/t/01-chowla.t | 12 ++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 challenge-109/lance-wicks/perl/ch-1.pl create mode 100644 challenge-109/lance-wicks/perl/lib/Chowla.pm create mode 100644 challenge-109/lance-wicks/perl/t/01-chowla.t diff --git a/challenge-109/lance-wicks/perl/ch-1.pl b/challenge-109/lance-wicks/perl/ch-1.pl new file mode 100644 index 0000000000..ddbb11ad8a --- /dev/null +++ b/challenge-109/lance-wicks/perl/ch-1.pl @@ -0,0 +1,12 @@ +use strict; +use warnings; + +use lib './lib'; +use Chowla; + +my $c = Chowla->new; +my $numbers = $c->list(20); + +for my $n (@$numbers) { + print "$n, "; +} diff --git a/challenge-109/lance-wicks/perl/lib/Chowla.pm b/challenge-109/lance-wicks/perl/lib/Chowla.pm new file mode 100644 index 0000000000..06619c38d2 --- /dev/null +++ b/challenge-109/lance-wicks/perl/lib/Chowla.pm @@ -0,0 +1,26 @@ +package Chowla; + +use Moo; + +sub list { + my ($self,$max) = @_; + my @chowla_numbers; + for my $n (1..$max) { + push @chowla_numbers, $self->n($n); + } + return \@chowla_numbers; +} + +sub n { + my ($self,$num) = @_; + + my $total = 0; + for my $n (2..$num-1) { + if ($num % $n == 0) { + $total += $n; + } + } + return $total; +} + +1; diff --git a/challenge-109/lance-wicks/perl/t/01-chowla.t b/challenge-109/lance-wicks/perl/t/01-chowla.t new file mode 100644 index 0000000000..15d50ac14e --- /dev/null +++ b/challenge-109/lance-wicks/perl/t/01-chowla.t @@ -0,0 +1,12 @@ +use Test2::V0 -target => 'Chowla'; + +is $CLASS->list(20),[0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21], 'List the first 20 Chowla numbers'; + +is $CLASS->n(1),0, 'First Chowla number should be zero'; +is $CLASS->n(2),0, 'Second Chowla number should be zero'; +is $CLASS->n(3),0, 'Third Chowla number should be zero'; +is $CLASS->n(4),2, 'Fourth Chowla number should be two'; +is $CLASS->n(5),0, 'Fifth Chowla number should be 0'; +is $CLASS->n(6),5, 'Sixth Chowla number should be 5'; + +done_testing; -- cgit From 97ed1c6391ff0077df0c446603be46db1e6bf219 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Wed, 21 Apr 2021 19:51:47 -0400 Subject: initial commit --- challenge-109/adam-russell/blog.txt | 0 challenge-109/adam-russell/blog1.txt | 0 challenge-109/adam-russell/perl/ch-1.pl | 0 challenge-109/adam-russell/perl/ch-2.pl | 0 challenge-109/adam-russell/prolog/ch-1.p | 0 challenge-109/adam-russell/prolog/ch-2.p | 0 6 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 challenge-109/adam-russell/blog.txt create mode 100644 challenge-109/adam-russell/blog1.txt create mode 100644 challenge-109/adam-russell/perl/ch-1.pl create mode 100644 challenge-109/adam-russell/perl/ch-2.pl create mode 100644 challenge-109/adam-russell/prolog/ch-1.p create mode 100644 challenge-109/adam-russell/prolog/ch-2.p diff --git a/challenge-109/adam-russell/blog.txt b/challenge-109/adam-russell/blog.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/adam-russell/blog1.txt b/challenge-109/adam-russell/blog1.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/adam-russell/perl/ch-1.pl b/challenge-109/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/adam-russell/perl/ch-2.pl b/challenge-109/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/adam-russell/prolog/ch-1.p b/challenge-109/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-109/adam-russell/prolog/ch-2.p b/challenge-109/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..e69de29bb2 -- cgit From afa7e43f4969e15ad2b07540e4fadf8e72b691dc Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 22 Apr 2021 14:58:53 +0200 Subject: AWK solution for week 109. part 2 --- challenge-109/abigail/README.md | 1 + challenge-109/abigail/awk/ch-2.awk | 126 +++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 challenge-109/abigail/awk/ch-2.awk diff --git a/challenge-109/abigail/README.md b/challenge-109/abigail/README.md index 1f7b55f195..3928e7302c 100644 --- a/challenge-109/abigail/README.md +++ b/challenge-109/abigail/README.md @@ -91,6 +91,7 @@ Output: ~~~~ ### Solutions +* [AWK](awk/ch-2.awk) * [Perl](perl/ch-2.pl) ### Blog 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] + } + } + } + } + } + } + } + } + } + } + } +} -- cgit From 105b08ecd006e8f7e5e6b23c2d4b15ab0e449725 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 22 Apr 2021 20:02:42 +0200 Subject: C solution for week 109, part 2 --- challenge-109/abigail/README.md | 2 + challenge-109/abigail/c/ch-2.c | 87 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 challenge-109/abigail/c/ch-2.c diff --git a/challenge-109/abigail/README.md b/challenge-109/abigail/README.md index 3928e7302c..815fe7fd8e 100644 --- a/challenge-109/abigail/README.md +++ b/challenge-109/abigail/README.md @@ -92,6 +92,8 @@ Output: ### Solutions * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) +* [C](c/ch-2.c) * [Perl](perl/ch-2.pl) ### Blog 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 +# include +# include + +/* + * 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]) { + continue; + } + for (size_t e_i = 0; e_i < SIZE; e_i ++) { + if (a_i == e_i || b_i == e_i || + c_i == e_i || d_i == e_i) { + continue; + } + for (size_t f_i = 0; f_i < SIZE; f_i ++) { + 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]) { + continue; + } + for (size_t g_i = 0; g_i < SIZE; g_i ++) { + 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]) { + continue; + } + 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]); + } + } + } + } + } + } + } + } + + free (line); + + return (0); +} -- cgit From c08186dfa8428ca980a8c66f71731a06aa47a3f0 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Fri, 23 Apr 2021 10:49:09 -0400 Subject: Perl and Prolog solutions --- challenge-109/adam-russell/perl/ch-1.pl | 29 ++++++++++++++ challenge-109/adam-russell/perl/ch-2.pl | 61 +++++++++++++++++++++++++++++ challenge-109/adam-russell/prolog/ch-1.p | 42 ++++++++++++++++++++ challenge-109/adam-russell/prolog/ch-2.p | 66 ++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+) diff --git a/challenge-109/adam-russell/perl/ch-1.pl b/challenge-109/adam-russell/perl/ch-1.pl index e69de29bb2..e5b82ec61f 100644 --- a/challenge-109/adam-russell/perl/ch-1.pl +++ b/challenge-109/adam-russell/perl/ch-1.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; +## +# Write a script to generate first 20 Chowla Numbers. +# C(n) = sum of divisors of n except 1 and n +## +use constant CHOWLA_COUNT => 20; +sub factor{ + my($n) = @_; + my @factors = (); + foreach my $j (2..sqrt($n)){ + push @factors, $j if $n % $j == 0; + push @factors, ($n / $j) if $n % $j == 0 && $j ** 2 != $n; + } + return @factors; +} + +sub chowla{ + my(@factors) = @_; + my $sum = unpack("%32I*", pack("I*", @factors)); +} + +MAIN:{ + my @chowla_numbers; + for my $n (1 .. CHOWLA_COUNT){ + push @chowla_numbers, chowla(factor($n)); + } + print join(", ", @chowla_numbers) . "\n"; +} \ No newline at end of file diff --git a/challenge-109/adam-russell/perl/ch-2.pl b/challenge-109/adam-russell/perl/ch-2.pl index e69de29bb2..3278d340e3 100644 --- a/challenge-109/adam-russell/perl/ch-2.pl +++ b/challenge-109/adam-russell/perl/ch-2.pl @@ -0,0 +1,61 @@ +use strict; +use warnings; +## +# You are given four squares as below with numbers named a,b,c,d,e,f,g. +# to place the given unique numbers in the square box so that sum of +# numbers in each box is the same. +# (1) (3) +# +--------------+ +--------------+ +# +# a e +# (2) (4) +# +---+------+---+ +---+---------+ +# +# b d f +# +# +# +----------+---+ +---+------+---+ +# c g +# +# +# +--------------+ +-------------+ +## +use AI::Prolog; + +my $prolog = do{ + local $/; + ; +}; +$prolog = new AI::Prolog($prolog); +$prolog->query("sums_in_squares([1,2,3,4,5,6,7], Squares)."); + +my $result; +while ($result = $prolog->results()){ + print join(",", @{$result->[2]}) . "\n"; +} + +__DATA__ +member(X,[X|T]). +member(X,[H|T]):- member(X,T). +sums_in_squares(Numbers, [A, B, C, D, E, F, G]):- + member(A, Numbers), + member(B, Numbers), + member(C, Numbers), + member(D, Numbers), + member(E, Numbers), + member(F, Numbers), + member(G, Numbers), + A \= B, A \= C, A \= D, A \= E, A \= F, A \= G, + B \= A, B \= C, B \= D, B \= E, B \= F, B \= G, + C \= A, C \= B, C \= D, C \= E, C \= F, C \= G, + D \= A, D \= B, D \= C, D \= E, D \= F, D \= G, + E \= A, E \= B, E \= C, E \= D, E \= F, E \= G, + F \= A, F \= B, F \= C, F \= D, F \= E, F \= G, + G \= A, G \= B, G \= C, G \= D, G \= E, G \= F, + Box1 is A + B, + Box2 is B + C + D, + Box3 is D + E + F, + Box4 is F + G, + Box1 == Box2, + Box2 == Box3, + Box3 == Box4. \ No newline at end of file diff --git a/challenge-109/adam-russell/prolog/ch-1.p b/challenge-109/adam-russell/prolog/ch-1.p index e69de29bb2..3e2458293b 100644 --- a/challenge-109/adam-russell/prolog/ch-1.p +++ b/challenge-109/adam-russell/prolog/ch-1.p @@ -0,0 +1,42 @@ +/* + Write a script to generate first 20 Chowla Numbers. + C(n) = sum of divisors of n except 1 and n +*/ + +:-initialization(main). + +print_with_comma([H|[]]):- + write(H), nl. +print_with_comma([H|T]):- + write(H), + write(', '), + print_with_comma(T). + +factor(N, Factors):- + S is round(sqrt(N)), + fd_domain(X, 2, S), + R #= N rem X, + R #= 0, + Q #= N // X, + Q #\= X, + fd_labeling([Q, X]), + Factors = [Q, X]. +factor(N, Factors):- + S is round(sqrt(N)), + fd_domain(X, 2, S), + R #= N rem X, + R #= 0, + Q #= N // X, + Q #= X, + fd_labeling([Q]), + Factors = [Q]. + +chowla(ChowlaNumber):- + between(1, 20, N), + findall(F, factor(N, F), Fs), + flatten(Fs, Factors), + sum_list(Factors, ChowlaNumber). + +main:- + findall(ChowlaNumber, chowla(ChowlaNumber), ChowlaNumbers), + print_with_comma(ChowlaNumbers). \ No newline at end of file diff --git a/challenge-109/adam-russell/prolog/ch-2.p b/challenge-109/adam-russell/prolog/ch-2.p index e69de29bb2..f56e7aa1a9 100644 --- a/challenge-109/adam-russell/prolog/ch-2.p +++ b/challenge-109/adam-russell/prolog/ch-2.p @@ -0,0 +1,66 @@ +/* + You are given four squares as below with numbers named a,b,c,d,e,f,g. + to place the given unique numbers in the square box so that sum of + numbers in each box is the same. + (1) (3) + +--------------+ +--------------+ + + a e + (2) (4) + +---+------+---+ +---+---------+ + + b d f + + + +----------+---+ +---+------+---+ + c g + + + +--------------+ +-------------+ +*/ + +:-initialization(main). + +all_unique(_, []). +all_unique(L, [V|T]) :- + fd_exactly(1, L, V), + all_unique(L, T). + +sums_in_squares_naive(Numbers, [A, B, C, D, E, F, G]):- + member(A, Numbers), + member(B, Numbers), + member(C, Numbers), + member(D, Numbers), + member(E, Numbers), + member(F, Numbers), + member(G, Numbers), + \+ (A == B; A == C; A == D; A == E; A == F; A == G), + \+ (B == A; B == C; B == D; B == E; B == F; B == G), + \+ (C == A; C == B; C == D; C == E; C == F; C == G), + \+ (D == A; D == B; D == C; D == E; D == F; D == G), + \+ (E == A; E == B; E == C; E == D; E == F; E == G), + \+ (F == A; F == B; F == C; F == D; F == E; F == G), + \+ (G == A; G == B; G == C; G == D; G == E; G == F), + Box1 is A + B, + Box2 is B + C + D, + Box3 is D + E + F, + Box4 is F + G, + Box1 == Box2, + Box2 == Box3, + Box3 == Box4. + +sums_in_squa