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 4a816dae782ebf95a65cb13fe7344e2664d926d3 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Wed, 21 Apr 2021 17:39:48 +0200 Subject: Task 1 done. --- challenge-109/luca-ferrari/raku/ch-1.p6 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-109/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-109/luca-ferrari/raku/ch-1.p6 b/challenge-109/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..820e7500c2 --- /dev/null +++ b/challenge-109/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#!perl6 + +# +# C(n) = sum of divisors of n except 1 and n +# +sub choowla( Int $n ) { + # get all divisors + + my @divisors; + for 2 .. $n / 2 { + @divisors.push: $_ if $n %% $_; + } + + return [+] @divisors; +} + +sub MAIN( Int $limit = 20 ) { + my @choowla-numbers.push: choowla( $_ ) for 1 .. $limit; + @choowla-numbers.join( ',' ).say; +} -- cgit From c6086e3997c25a9398a791fb962f821b7b796dab Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Wed, 21 Apr 2021 17:40:50 +0200 Subject: More compact way of doing divisors. --- challenge-109/luca-ferrari/raku/ch-1.p6 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-109/luca-ferrari/raku/ch-1.p6 b/challenge-109/luca-ferrari/raku/ch-1.p6 index 820e7500c2..c480b09dfc 100644 --- a/challenge-109/luca-ferrari/raku/ch-1.p6 +++ b/challenge-109/luca-ferrari/raku/ch-1.p6 @@ -6,10 +6,10 @@ sub choowla( Int $n ) { # get all divisors - my @divisors; - for 2 .. $n / 2 { - @divisors.push: $_ if $n %% $_; - } + my @divisors.push: $_ if $n %% $_ for 2 .. $n / 2; + # for 2 .. $n / 2 { + # @divisors.push: $_ if $n %% $_; + # } return [+] @divisors; } -- cgit From 342181744b067fd36499de15529b2f87443db79f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Wed, 21 Apr 2021 18:16:58 +0200 Subject: Task2 done --- challenge-109/luca-ferrari/raku/ch-2.p6 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-109/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-109/luca-ferrari/raku/ch-2.p6 b/challenge-109/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..1213d407ca --- /dev/null +++ b/challenge-109/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,30 @@ +#!raku + + +sub MAIN() { + my @nums = <1 2 3 4 5 6 7>; + my @letters = ; + + + my @solutions; + + for @nums.permutations -> @current { + my %squares; + %squares{ @letters[ $_ ] } = @current[ $_ ] for 0 ..^ @letters.elems; + + + my @sums; + + @sums.push: [+] %squares; + @sums.push: [+] %squares; + @sums.push: [+] %squares; + @sums.push: [+] %squares; + + @solutions.push: %squares if @sums[ 0 ] == all @sums; + } + + + + say $_ for @solutions; + +} -- cgit From 9b844027cb11bf15f01c897e5d45b44b2387fb8e Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Wed, 21 Apr 2021 18:20:10 +0200 Subject: Command line checks --- challenge-109/luca-ferrari/raku/ch-2.p6 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-109/luca-ferrari/raku/ch-2.p6 b/challenge-109/luca-ferrari/raku/ch-2.p6 index 1213d407ca..15cc86739b 100644 --- a/challenge-109/luca-ferrari/raku/ch-2.p6 +++ b/challenge-109/luca-ferrari/raku/ch-2.p6 @@ -1,25 +1,25 @@ #!raku -sub MAIN() { - my @nums = <1 2 3 4 5 6 7>; +sub MAIN( *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems && @nums.elems == 7 } ) { my @letters = ; - - my @solutions; + # permutate all numbers to find out the correct sum for @nums.permutations -> @current { + # build an hash to help visualize the answer my %squares; %squares{ @letters[ $_ ] } = @current[ $_ ] for 0 ..^ @letters.elems; - + # compute the sums my @sums; - @sums.push: [+] %squares; @sums.push: [+] %squares; @sums.push: [+] %squares; @sums.push: [+] %squares; + + # if the first sum is equal to all the others, push this solution @solutions.push: %squares if @sums[ 0 ] == all @sums; } -- cgit From 16814906f220961e56b95cf544b20bf1ac16e890 Mon Sep 17 00:00:00 2001 From: Philip Hood Date: Wed, 21 Apr 2021 13:42:13 -0400 Subject: challenge 109, in raku --- challenge-109/pkmnx/raku/ch-1.raku | 6 ++++++ challenge-109/pkmnx/raku/ch-2.raku | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100755 challenge-109/pkmnx/raku/ch-1.raku create mode 100755 challenge-109/pkmnx/raku/ch-2.raku diff --git a/challenge-109/pkmnx/raku/ch-1.raku b/challenge-109/pkmnx/raku/ch-1.raku new file mode 100755 index 0000000000..4d31c01eed --- /dev/null +++ b/challenge-109/pkmnx/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku + + +my $upr = 20; +my @chwla = (1 .. $upr).map(-> $n { (2..$n -1).grep({ !($n % $_) }).sum() }); +@chwla.say; diff --git a/challenge-109/pkmnx/raku/ch-2.raku b/challenge-109/pkmnx/raku/ch-2.raku new file mode 100755 index 0000000000..f71cf17e84 --- /dev/null +++ b/challenge-109/pkmnx/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku + +# See usage: +# pk@pkx:~/stuff/.../challenge-109/pkmnx/raku$ time ./ch-2.raku 1 2 3 5 4 6 7 +# 3 + 7 == 7 + 2 + 1 == 1 + 5 + 4 == 4 + 6 +# 5 + 6 == 6 + 2 + 3 == 3 + 1 + 7 == 7 + 4 +# 4 + 5 == 5 + 3 + 1 == 1 + 6 + 2 == 2 + 7 +# 4 + 7 == 7 + 1 + 3 == 3 + 2 + 6 == 6 + 5 +# 6 + 4 == 4 + 1 + 5 == 5 + 2 + 3 == 3 + 7 +# 6 + 4 == 4 + 5 + 1 == 1 + 2 + 7 == 7 + 3 +# 7 + 2 == 2 + 6 + 1 == 1 + 3 + 5 == 5 + 4 +# 7 + 3 == 3 + 2 + 5 == 5 + 1 + 4 == 4 + 6 +# +# real 0m0.337s + +sub MAIN( *@N where @N.elems == 7 && @N.all ~~ Numeric ) { + + @N.permutations.grep({ val(|$_) }).map({ disp(|$_) }).join("\n").say; + +} + +sub val ( $a, $b, $c, $d, $e, $f, $g ) { + $a + $b == $b + $c + $d == $d + $e + $f == $f + $g; +} +sub disp ( $a, $b, $c, $d, $e, $f, $g ) { + "$a + $b == $b + $c + $d == $d + $e + $f == $f + $g"; +} -- cgit From db1fc1a584d92aa682901a624f4b105014dce916 Mon Sep 17 00:00:00 2001 From: PJ Durai Date: Wed, 21 Apr 2021 11:38:46 -0700 Subject: Week 109 Raku --- challenge-109/pjdurai/raku/ch-1.raku | 25 +++++++++++++++++++++++++ challenge-109/pjdurai/raku/ch-2.raku | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 challenge-109/pjdurai/raku/ch-1.raku create mode 100644 challenge-109/pjdurai/raku/ch-2.raku diff --git a/challenge-109/pjdurai/raku/ch-1.raku b/challenge-109/pjdurai/raku/ch-1.raku new file mode 100755 index 0000000000..f66ba47b0b --- /dev/null +++ b/challenge-109/pjdurai/raku/ch-1.raku @@ -0,0 +1,25 @@ +#! /usr/bin/env raku + +# Returns a list of divisors except 1 and Self. + +sub get-divisors ($num){ + reduce sub (@acc,$i){ + if $num %% $i && $i != 1 && $i != $num { + @acc.push($i) ; + my $m = $num / $i; + @acc.push($m) if $m != $i; + } + @acc; + }, [], | (1..$num.sqrt.floor); +} + +# Print 20 Chowla Numbers. +# To input the count do + +# 'raku ch-1.raku --N=25' + +sub MAIN( + Int :$N = 20, +) { + say ([1..$N].map( {[+] get-divisors($_)} )).join(","); +} diff --git a/challenge-109/pjdurai/raku/ch-2.raku b/challenge-109/pjdurai/raku/ch-2.raku new file mode 100644 index 0000000000..65b4e2e906 --- /dev/null +++ b/challenge-109/pjdurai/raku/ch-2.raku @@ -0,0 +1,34 @@ +#! /usr/bin/env raku + +# Call as 'raku ch-2.raku --numbers="1,2,3,4,5,6,7"' +# Defaults to "1,2,3,4,5,6,7" +# +# The problme has multiple solutions. It prints all solutions. + +sub MAIN( + Str :$numbers = "1,2,3,4,5,6,7" +){ + my @labels = ["a".."z"]; + my @arr = $numbers.split(/\,/); + + my @solutions = @arr.permutations.grep( + ->@l + { + @l[0] + @l[1] == @l[1] + @l[2] + @l[3] == @l[3] + @l[4] + @l[5] == @l[5] + @l[6]; + + }); + + for @solutions -> @solution { + say "Solution: ", @solution; + for zip(@labels, @solution) -> @pair { + say "@pair[0] = @pair[1]"; + } + say ""; + say "Box 1: a + b = @solution[0] + @solution[1] = ", @solution[0] + @solution[1]; + say "Box 2: b + c + d = @solution[1] + @solution[2] + @solution[3] = ", @solution[1] + @solution[2] + @solution[3]; + say "Box 3: d + e + f = @solution[3] + @solution[4] + @solution[5] = ", @solution[3] + @solution[4] + @solution[5]; + say "Box 4: a + b = @solution[5] + @solution[6] = ", @solution[5] + @solution[6]; + say "---------\n"; + + } +} -- 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 fccecc2765b5344e925e4379d3c822fbb674aa97 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 19 Apr 2021 17:44:14 +0200 Subject: Solution to task 1 --- challenge-109/jo-37/perl/ch-1.pl | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 challenge-109/jo-37/perl/ch-1.pl diff --git a/challenge-109/jo-37/perl/ch-1.pl b/challenge-109/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..fd3ca1d792 --- /dev/null +++ b/challenge-109/jo-37/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util 'divisor_sum'; +use experimental 'signatures'; + +our $examples; + +run_tests() if $examples; # does not return + +die < 1); +} + + +### Examples and tests + +sub run_tests { + my $n; + is chowla(++$n), $_, "chowla($n) = $_" for + (0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21); + + done_testing; + exit; +} -- cgit From 60f434a87bbb5e4156fa7f83f6208a9cb02627e3 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 19 Apr 2021 19:06:28 +0200 Subject: Solution to task 2 --- challenge-109/jo-37/perl/ch-2.pl | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 challenge-109/jo-37/perl/ch-2.pl diff --git a/challenge-109/jo-37/perl/ch-2.pl b/challenge-109/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..12a970bf66 --- /dev/null +++ b/challenge-109/jo-37/perl/ch-2.pl @@ -0,0 +1,92 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util qw(forperm); +use List::Util qw(sum uniqnum); +use experimental qw(signatures); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <>>>> @>>>>> @>>>>> @>>>>> @>>>>> @>>>>> @>>>>> +@$_ +. + +write for @{puzzle(@ARGV)}; + + +### Implementation + +# A brute force approach: +# Iterate over all permutations of the given numbers, assign them to the +# boxes and select those having equal box sums. + +# index-to-box assignments: +use constant BOX => ([0, 1], [1, 2, 3], [3, 4, 5], [5, 6]); + +sub puzzle (@num) { + + # Iterate over all permutations. + my @puzzle; + forperm { + # Sum over the the numbers contained in each box and count the + # unique values thereof. Collect a solution if the sums consist + # of a single value. + # Double slice: pick the indices belonging to the current + # box from the permutation and then collect the respective + # numbers. + push @puzzle, [@num[@_]] + if uniqnum(map {sum @num[@_[@$_]]} BOX) == 1; + } @num; + + \@puzzle; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + is puzzle(1 .. 7), bag {item [qw(6 4 1 5 2 3 7)]; etc}, 'example'; + } + + SKIP: { + skip "tests" unless $tests; + + # No two pairs have the same sum, thus there cannot be a solution: + is puzzle(1, 2, 4, 8, 16, 32, 64), [], 'no solution'; + + is puzzle(-1.1, 0, 1.1, 2.2, 3.3, 4.4, 5.5), + bag {item [3.3, 2.2, -1.1, 4.4, 1.1, 0, 5.5]; etc}, + 'include negative floating point numbers'; + } + + done_testing; + exit; +} -- cgit From aa5f2d60c7582a0186443d6e9e2d763b0bfb5d68 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Wed, 21 Apr 2021 16:33:51 -0400 Subject: Challenge 109 in Perl and (some) JS, Blogged --- challenge-109/dave-jacoby/blog.txt | 1 + challenge-109/dave-jacoby/node/ch-1.js | 17 ++++++++ challenge-109/dave-jacoby/perl/ch-1.pl | 35 +++++++++++++++ challenge-109/dave-jacoby/perl/ch-2.pl | 79 ++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 challenge-109/dave-jacoby/blog.txt create mode 100644 challenge-109/dave-jacoby/node/ch-1.js create mode 100644 challenge-109/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-109/dave-jacoby/perl/ch-2.pl diff --git a/challenge-109/dave-jacoby/blog.txt b/challenge-109/dave-jacoby/blog.txt new file mode 100644 index 0000000000..98a826c8ac --- /dev/null +++ b/challenge-109/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/04/21/iterations-and-permutations-perl-weekly-challenge-109.html \ No newline at end of file diff --git a/challenge-109/dave-jacoby/node/ch-1.js b/challenge-109/dave-jacoby/node/ch-1.js new file mode 100644 index 0000000000..72452fdcb5 --- /dev/null +++ b/challenge-109/dave-jacoby/node/ch-1.js @@ -0,0 +1,17 @@ +"use strict"; + +let list = Array(20) + .fill() + .map((x, i) => i + 1) + .map((x) => chowla(x)); +console.log(list.join(", ")); + +function chowla(n) { + return Array(n) + .fill() + .map((x, i) => i + 1) + .filter((x) => x != 1) + .filter((x) => x != n) + .filter((x) => n % x == 0) + .reduce((a, v) => a + v, 0); +} diff --git a/challenge-109/dave-jacoby/perl/ch-1.pl b/challenge-109/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..96de494c1a --- /dev/null +++ b/challenge-109/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ postderef say signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{sum0}; + +my @test = ( 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 ); + +say join ', ', @test; +say join ', ', map { chowla($_) } 1 .. 20; +say join ', ', map { chowla2($_) } 1 .. 20; + +# the old-fashioned for-loop way +sub chowla ( $n ) { + my $c = 0; + for my $i ( 1 .. $n ) { + my $m = $n % $i; + next if $i == 1 || $i == $n; + $c += $i if $m == 0; + } + return $c; +} + +# the new, hot functional approach +# using sum0 because if given an empty list, +# sum0 returns zero instead of undef +sub chowla2 ( $n ) { + return sum0 + grep { $n % $_ == 0 } + grep { $_ != 1 } + grep { $_ != $n } 1 .. $n; +} diff --git a/challenge-109/dave-jacoby/perl/ch-2.pl b/challenge-109/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..d1dba3614c --- /dev/null +++ b/challenge-109/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ postderef say signatures state }; +no warnings qw{ experimental }; + +use Algorithm::Permute; + +my $map = <<'END'; + (1) (3) + ╔══════════════╗ ╔══════════════╗ + ║ ║ ║ ║ + ║ a ║ ║ e ║ + ║ ║ (2) ║ ║ (4) + ║ ┌───╫──────╫───┐ ┌───╫─────────┐ + ║ │ ║ ║ │ │ ║ │ + ║ │ b ║ ║ d │ │ f ║ │ + ║ │ ║ ║ │ │ ║ │ + ║ │ ║ ║ │ │ ║ │ + ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │ + │ c │ │ g │ + │ │ │ │ + │ │ │ │ + └──────────────┘ └─────────────┘ +END + +four_squares( 1 .. 7 ); + +sub four_squares ( @array ) { + my $array = join ', ', @array; + my $ap = Algorithm::Permute->new( \@array ); + while ( my @perm = $ap->next ) { + my $b1 = _box_1(@perm); + my $b2 = _box_2(@perm); + next if $b1 != $b2; + my $b3 = _box_3(@perm); + next if $b1 != $b3; + my $b4 = _box_4(@perm); + next if $b1 != $b4; + + my $a = $perm[0]; + my $b = $perm[1]; + my $c = $perm[2]; + my $d = $perm[3]; + my $e = $perm[4]; + my $f = $perm[5]; + my $g = $perm[6]; + + say <<"END"; + a = $a e = $e + b = $b f = $f + c = $c g = $g + d = $d + Box1 = a + b = $a + $b = $b1 + Box2 = b + c + d = $b + $c + $d = $b2 + Box3 = d + e + f = $d + $e + $f = $b3 + Box4 = f + g = $f + $g = $b4 +END + + } + +} + +sub _box_1( @array ) { + return $array[0] + $array[1]; +} + +sub _box_2( @array ) { + return $array[1] + $array[2] + $array[3]; +} + +sub _box_3( @array ) { + return $array[3] + $array[4] + $array[5]; +} + +sub _box_4( @array ) { + return $array[5] + $array[6]; +} -- 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 dif