From bd82e6dfcf2d047c7916d67e8d931eeec757998a Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 Jan 2022 15:56:40 +0100 Subject: Week 147. Perl solutions --- challenge-147/abigail/perl/ch-1.pl | 49 +++++++++++++++++++++++++++++++ challenge-147/abigail/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++++++ challenge-147/abigail/t/ctest.ini | 8 +++++ challenge-147/abigail/t/input-1-1 | 0 challenge-147/abigail/t/input-2-1 | 0 challenge-147/abigail/t/output-1-1.exp | 1 + challenge-147/abigail/t/output-2-1.exp | 1 + 7 files changed, 112 insertions(+) create mode 100644 challenge-147/abigail/perl/ch-1.pl create mode 100644 challenge-147/abigail/perl/ch-2.pl create mode 100644 challenge-147/abigail/t/ctest.ini create mode 100644 challenge-147/abigail/t/input-1-1 create mode 100644 challenge-147/abigail/t/input-2-1 create mode 100644 challenge-147/abigail/t/output-1-1.exp create mode 100644 challenge-147/abigail/t/output-2-1.exp diff --git a/challenge-147/abigail/perl/ch-1.pl b/challenge-147/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..4e75524897 --- /dev/null +++ b/challenge-147/abigail/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: perl ch-1.pl +# + +# +# This is asking for the first 20 terms of A024785, which we can just +# grab from https://oeis.org/A024785/list. +# +# Or we port the Python code from https://oeis.org/A024785 +# + +use Math::Prime::Util qw [is_prime]; + +my @todo = qw [2 3 5 7]; +my $count = 20; + +print "$_ " for @todo; +$count -= @todo; + +MAIN: while (@todo) { + my @next; + for my $d (1 .. 9) { + foreach my $p (@todo) { + my $candidate = "$d$p"; + next unless is_prime ($candidate); + print "$candidate "; + last MAIN if -- $count <= 0; + push @next => $candidate; + } + } + @todo = @next; +} + +print "\n"; diff --git a/challenge-147/abigail/perl/ch-2.pl b/challenge-147/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..e4ed1d17e9 --- /dev/null +++ b/challenge-147/abigail/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: perl ch-2.pl +# + +# +# We solve this by checking every pentagon number in order to see +# whether it can be written as a sum of two (smaller) pentagon +# numbers. If so, if their difference is a pentagon number, we +# have the winning pair. We'll use a hash which tracks the pentagon +# numbers seen so far. +# +# To calculate the next pentagon number, we will avoid division +# (and multiplication). +# +# If P (N) == N * (3 * N - 1) / 2, then +# +# P (N + 1) == (N + 1) * (3 * (N + 1) - 1) / 2 +# == ((N + 1) * (3 * N + 2) / 2 +# == (3 * N * N + 5 * N + 2) / 2 +# == (3 * N * N - N + 6 * N + 2) / 2 +# == (N * (3 * N - 1) / 2) + ((6 * N + 2) / 2) +# == P (N) + 3 * N + 1 +# == P (N) + N + N + N + 1 +# + +my %pentagon; + +MAIN: for (;;) { + state $n = 0; + state $p = 0; + $pentagon {$p += $n + $n + $n ++ + 1} ++; + + $_ + $_ < $p && $pentagon {$p - $_} + && $pentagon {$p - $_ - $_} + && say ("$_ ", $p - $_) + && last MAIN for keys %pentagon; +} + diff --git a/challenge-147/abigail/t/ctest.ini b/challenge-147/abigail/t/ctest.ini new file mode 100644 index 0000000000..bfef483d85 --- /dev/null +++ b/challenge-147/abigail/t/ctest.ini @@ -0,0 +1,8 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Fixed Output +2-1 = Fixed Output diff --git a/challenge-147/abigail/t/input-1-1 b/challenge-147/abigail/t/input-1-1 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-147/abigail/t/input-2-1 b/challenge-147/abigail/t/input-2-1 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-147/abigail/t/output-1-1.exp b/challenge-147/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..3648c94b73 --- /dev/null +++ b/challenge-147/abigail/t/output-1-1.exp @@ -0,0 +1 @@ +2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197 diff --git a/challenge-147/abigail/t/output-2-1.exp b/challenge-147/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..de3d6ccd8a --- /dev/null +++ b/challenge-147/abigail/t/output-2-1.exp @@ -0,0 +1 @@ +1560090 7042750 -- cgit From 3617a4290d0ceff2af483943e103d3ec71ae2874 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 Jan 2022 17:46:33 +0100 Subject: Week 147: awk solutions --- challenge-147/abigail/awk/ch-1.awk | 52 ++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/awk/ch-2.awk | 26 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 challenge-147/abigail/awk/ch-1.awk create mode 100644 challenge-147/abigail/awk/ch-2.awk diff --git a/challenge-147/abigail/awk/ch-1.awk b/challenge-147/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..c39dc35247 --- /dev/null +++ b/challenge-147/abigail/awk/ch-1.awk @@ -0,0 +1,52 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: awk -f ch-1.awk +# + +func is_prime (n, i) { + if (n % 2 == 0) {return 0} + for (i = 3; i <= sqrt (n); i += 2) { + if (n % i == 0) { + return 0 + } + } + return 1 +} + +BEGIN { + todo [1] = 2 + todo [2] = 3 + todo [3] = 5 + todo [4] = 7 + n = 4 + i = 1 + count = 20 - n; + + printf ("2 3 5 7 ") + + low = 1 + high = n + while (count > 0) { + for (d = 1; d <= 9 && count > 0; d ++) { + for (i = low; i <= high && count > 0; i ++) { + candidate = d todo [i] + if (is_prime(candidate) == 1) { + printf candidate " " + count --; + todo [++ n] = candidate; + } + } + } + low = high + 1 + high = n + } + printf ("\n") +} + + + diff --git a/challenge-147/abigail/awk/ch-2.awk b/challenge-147/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..e071da78fc --- /dev/null +++ b/challenge-147/abigail/awk/ch-2.awk @@ -0,0 +1,26 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: awk -f ch-2.awk +# + +BEGIN { + while (!done) { + p += n + n + n ++ + 1 + pentagon [p] = 1 + + for (seen in pentagon) { + if (seen + seen < p && (p - seen) in pentagon \ + && (p - seen - seen) in pentagon ) { + print seen, p - seen + done = 1 + break + } + } + } +} + -- cgit From 354215f8375796503461e4cb1d6cde02a566c4cf Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 Jan 2022 20:06:43 +0100 Subject: Week 147: bash solutions --- challenge-147/abigail/bash/ch-1.sh | 55 ++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/bash/ch-2.sh | 30 +++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-147/abigail/bash/ch-1.sh create mode 100644 challenge-147/abigail/bash/ch-2.sh diff --git a/challenge-147/abigail/bash/ch-1.sh b/challenge-147/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..1f5f09be50 --- /dev/null +++ b/challenge-147/abigail/bash/ch-1.sh @@ -0,0 +1,55 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: bash ch-1.sh +# + +set -f + +function is_prime () { + local n=$1 + local i + is_prime=1 + if ((n > 2 && n % 2 == 0)) + then is_prime=0 + else for ((i = 3; i * i <= n && is_prime == 1; i += 2)) + do if ((n % i == 0)) + then is_prime=0 + fi + done + fi +} + +todo=(2 3 5 7) +count=20 + +for p in "${todo[@]}" +do printf "$p " + ((count --)) +done + + +while ((count > 0)) +do next=() + for ((d = 1; d <= 9 && count > 0; d ++)) + do for p in "${todo[@]}" + do candidate=$d$p + is_prime $candidate + if (($is_prime == 1)) + then printf $candidate" " + ((count --)) + next+=($candidate) + if ((count <= 0)) + then break + fi + fi + done + done + todo=("${next[@]}") +done + +echo diff --git a/challenge-147/abigail/bash/ch-2.sh b/challenge-147/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..f38223377d --- /dev/null +++ b/challenge-147/abigail/bash/ch-2.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: bash ch-2.sh +# + +set -f + +declare -A pentagon + +# ((n = 0)) +# ((p = 0)) + +while true +do ((p = p + n + n + n + 1)) + ((n ++)) + pentagon[$p]=1 + + for seen in "${!pentagon[@]}" + do if ((seen + seen < p)) && [ -v pentagon[$((p - seen))] -a \ + -v pentagon[$((p - seen - seen))] ] + then echo $seen" "$((p - seen)) + break 2 + fi + done +done -- cgit From 95d45c5753b63b7656a434973453dae12e227a6a Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 11 Jan 2022 01:12:49 +0100 Subject: Week 147: bc solutions --- challenge-147/abigail/bc/ch-1.bc | 57 ++++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/bc/ch-2.bc | 41 +++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 challenge-147/abigail/bc/ch-1.bc create mode 100644 challenge-147/abigail/bc/ch-2.bc diff --git a/challenge-147/abigail/bc/ch-1.bc b/challenge-147/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..52d5cdb0f0 --- /dev/null +++ b/challenge-147/abigail/bc/ch-1.bc @@ -0,0 +1,57 @@ +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: bc ch-1.bc +# + +define is_prime (p) { + auto d + if (p == 2) {return 1} + if (p % 2 == 0) {return 0} + for (d = 3; d * d <= p; d += 2) { + if (p % d == 0) {return 0} + } + return 1 +} + +todo [1] = 2 +todo [2] = 3 +todo [3] = 5 +todo [4] = 7 +high = 4 + +for (i = 1; i <= high; i ++) { + print todo [i], " " +} + +count = 20 - high + +pow = 10 +while (count > 0) { + new_high = 0 + for (d = 1; d <= 9 && count > 0; d ++) { + for (i = 1; i <= high && count > 0; i ++) { + candidate = d * pow + todo [i] + if (is_prime (candidate)) { + print candidate, " " + new_high = new_high + 1 + count = count - 1 + next [new_high] = candidate + } + } + } + for (j = 1; j <= new_high; j ++) { + todo [j] = next [j] + } + high = new_high + pow = pow * 10 +} + +" +" + +halt diff --git a/challenge-147/abigail/bc/ch-2.bc b/challenge-147/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..6aee8ec9b4 --- /dev/null +++ b/challenge-147/abigail/bc/ch-2.bc @@ -0,0 +1,41 @@ +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: bc ch-2.bc +# + +define is_pentagonal (p) { + auto l, h, m + l = l + h = high + while (l <= h) { + m = (l + h) / 2 + if (pentagon [m] == p) {return 1} + if (pentagon [m] < p) {l = m + 1} else {h = m - 1} + } + return 0 +} + +while (!done) { + p = p + n + n + n + 1 + n = n + 1 + high = high + 1 + + pentagon [high] = p + + for (i = 1; i <= high && pentagon [i] + pentagon [i] < p && !done; i ++) { + seen = pentagon [i] + + if (is_pentagonal (p - seen) && is_pentagonal (p - seen - seen)) { + print seen, " " + p - seen + done = 1 + } + } +} + +halt -- cgit From 46a9f55050cb852410da9c80c7e248d8df0328a8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 11 Jan 2022 19:04:33 +0100 Subject: Week 147: C solutions --- challenge-147/abigail/c/ch-1.c | 70 ++++++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/c/ch-2.c | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 challenge-147/abigail/c/ch-1.c create mode 100644 challenge-147/abigail/c/ch-2.c diff --git a/challenge-147/abigail/c/ch-1.c b/challenge-147/abigail/c/ch-1.c new file mode 100644 index 0000000000..d02433f475 --- /dev/null +++ b/challenge-147/abigail/c/ch-1.c @@ -0,0 +1,70 @@ +# include +# include +# include +# include + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o + */ +# define COUNT 20 + +bool is_prime (int n) { + if (n % 2 == 0) {return n == 2;} + for (int i = 3; i * i <= n; i += 2) { + if (n % i == 0) {return false;} + } + return true; +} + +int main (void) { + int * todo; + if ((todo = (int *) malloc (4 * sizeof (int))) == NULL) { + perror ("Malloc failed"); + return (1); + } + todo [0] = 2; + todo [1] = 3; + todo [2] = 5; + todo [3] = 7; + size_t high = 4; + + for (size_t i = 0; i < high; i ++) { + printf ("%d ", todo [i]); + } + + int count = COUNT - high; + + int pow = 10; + while (count > 0) { + int * next; + int next_high = 0; + if ((next = (int *) malloc (9 * high * sizeof (int))) == NULL) { + perror ("Malloc failed"); + return (1); + } + for (int d = 1; d <= 9 && count > 0; d ++) { + for (size_t i = 0; i < high && count > 0; i ++) { + int candidate = d * pow + todo [i]; + if (is_prime (candidate)) { + next [next_high ++] = candidate; + printf ("%d ", candidate); + count --; + } + } + } + if (!next_high) { + break; + } + free (todo); + todo = next; + high = next_high; + pow *= 10; + } + free (todo); + + printf ("\n"); +} diff --git a/challenge-147/abigail/c/ch-2.c b/challenge-147/abigail/c/ch-2.c new file mode 100644 index 0000000000..02ff39bd1e --- /dev/null +++ b/challenge-147/abigail/c/ch-2.c @@ -0,0 +1,66 @@ +# include +# include +# include +# include + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o + */ + +bool is_pentagonal (int candidate, int * pentagon, size_t max) { + size_t low = 0; + size_t high = max; + while (low < high) { + size_t mid = (low + high) / 2; + if (pentagon [mid] == candidate) {return true;} + if (pentagon [mid] < candidate) {low = mid + 1;} + if (pentagon [mid] > candidate) {high = mid;} + } + return false; +} + + +int main (void) { + int * pentagon = NULL; + int n = 0; + int p = 0; + size_t cap = 100; + size_t high = 0; + bool done = false; + + if ((pentagon = (int *) malloc (cap * sizeof (int))) == NULL) { + perror ("Malloc failed"); + return (1); + } + + while (!done) { + p += n + n + n + 1; + n ++; + if (high >= cap) { + cap *= 2; + if ((pentagon = + (int *) realloc (pentagon, cap * sizeof (int))) == NULL) { + perror ("Realloc failed"); + return (1); + } + } + pentagon [high ++] = p; + + for (size_t i = 0; i < high && + pentagon [i] + pentagon [i] < p && + !done; i ++) { + int seen = pentagon [i]; + if (is_pentagonal (p - seen, pentagon, high) && + is_pentagonal (p - seen - seen, pentagon, high)) { + printf ("%d %d\n", seen, p - seen); + done = true; + } + } + } + + return (0); +} -- cgit From 94cdbab3f1198c78a3dcedb9ada1a8596114cfcb Mon Sep 17 00:00:00 2001 From: drbaggy Date: Wed, 12 Jan 2022 01:48:10 +0000 Subject: last unless replaced by ||last --- challenge-147/james-smith/README.md | 4 ++-- challenge-147/james-smith/perl/ch-1-right.pl | 2 +- challenge-147/james-smith/perl/ch-1.pl | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-147/james-smith/README.md b/challenge-147/james-smith/README.md index 6941c1b663..13e84e0c1b 100644 --- a/challenge-147/james-smith/README.md +++ b/challenge-147/james-smith/README.md @@ -49,7 +49,7 @@ my @tprimes_current = (3,7); printf $TEMPLATE, ++$index, $_, time-$t0 for 2,3,5,7; while(1) { # ** Stop if there are no primes of length `n`. - last unless @tprimes_current; + @tprimes_current||last; my @tprimes_new = (); # ** Loop through each of possible left-truncatable primes... for my $first ( 1..9 ) { @@ -109,7 +109,7 @@ my @tprimes_current = (2,3,5,7); printf $TEMPLATE, ++$idx, $_, time - $t0 foreach @tprimes_current; for my $p ( 1 .. 100 ) { - last unless @tprimes_current; + @tprimes_current||last; my @tprimes_new = (); foreach my $base ( @tprimes_current ) { B: foreach my $last ( 1,3,7,9 ) { diff --git a/challenge-147/james-smith/perl/ch-1-right.pl b/challenge-147/james-smith/perl/ch-1-right.pl index 9565a26b0f..ef094dcf7b 100644 --- a/challenge-147/james-smith/perl/ch-1-right.pl +++ b/challenge-147/james-smith/perl/ch-1-right.pl @@ -12,7 +12,7 @@ my @tprimes_current = (2,3,5,7); printf $TEMPLATE, ++$idx, $_, time - $t0 foreach @tprimes_current; for my $p ( 1 .. 100 ) { - last unless @tprimes_current; + @tprimes_current||last; my @tprimes_new = (); foreach my $base ( @tprimes_current ) { B: foreach my $last ( 1,3,7,9 ) { diff --git a/challenge-147/james-smith/perl/ch-1.pl b/challenge-147/james-smith/perl/ch-1.pl index 3d9b2ccf72..2f0c0641cb 100644 --- a/challenge-147/james-smith/perl/ch-1.pl +++ b/challenge-147/james-smith/perl/ch-1.pl @@ -34,8 +34,8 @@ my @tprimes_current = (3,7); printf $T, ++$index, $_, time-$t0 for 2,3,5,7; while(1) { - last unless @tprimes_current; ## Exit if there are no current l-trunc primes - ## We are at the end of the list... + @tprimes_current||last; ## Exit if there are no current l-trunc primes + ## We are at the end of the list... my @tprimes_new = (); -- cgit From a3f595d255809dfb78e43d0179cbefd67a5a1c62 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 15 Jan 2022 16:30:51 +0100 Subject: Week 147: Go solutions --- challenge-147/abigail/go/ch-1.go | 56 ++++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/go/ch-2.go | 31 ++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 challenge-147/abigail/go/ch-1.go create mode 100644 challenge-147/abigail/go/ch-2.go diff --git a/challenge-147/abigail/go/ch-1.go b/challenge-147/abigail/go/ch-1.go new file mode 100644 index 0000000000..a71d770cb3 --- /dev/null +++ b/challenge-147/abigail/go/ch-1.go @@ -0,0 +1,56 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" +) + +func is_prime (num int) bool { + if num == 2 { + return true + } + if num % 2 == 0 { + return false + } + for i := 3; i * i <= num; i += 2 { + if num % i == 0 { + return false + } + } + return true +} + +func main () { + todo := [] int {2, 3, 5, 7} + for _, n := range todo { + fmt . Printf ("%d ", n) + } + count := 20 - len (todo) + pow := 10 + for count > 0 && len (todo) > 0 { + new_todo := [] int {} + for d := 1; (d <= 9) && (count > 0); d ++ { + for _, p := range (todo) { + candidate := d * pow + p + if is_prime (candidate) { + fmt . Printf ("%d ", candidate) + new_todo = append (new_todo, candidate) + count -- + } + if count <= 0 { + break + } + } + } + todo = new_todo + pow *= 10 + } + fmt . Print ("\n") +} diff --git a/challenge-147/abigail/go/ch-2.go b/challenge-147/abigail/go/ch-2.go new file mode 100644 index 0000000000..13fe915ad4 --- /dev/null +++ b/challenge-147/abigail/go/ch-2.go @@ -0,0 +1,31 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +// + +// +// Run as: go run ch-2.go +// + +import ( + "fmt" +) + +func main () { + p := 0 + n := 0 + pentagon := map [int] bool { } + for { + p = p + n + n + n + 1 + n ++ + pentagon [p] = true + for seen := range pentagon { + if seen + seen < p && pentagon [p - seen] && + pentagon [p - seen - seen] { + fmt . Printf ("%d %d\n", seen, p - seen) + return + } + } + } +} -- cgit From f6b8071c5ec3c2470b8e1ba37908648e0dbededc Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 15 Jan 2022 17:17:33 +0100 Subject: Week 147: Java solutions --- challenge-147/abigail/java/ch-1.java | 54 ++++++++++++++++++++++++++++++++++++ challenge-147/abigail/java/ch-2.java | 32 +++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 challenge-147/abigail/java/ch-1.java create mode 100644 challenge-147/abigail/java/ch-2.java diff --git a/challenge-147/abigail/java/ch-1.java b/challenge-147/abigail/java/ch-1.java new file mode 100644 index 0000000000..9b3b57be9e --- /dev/null +++ b/challenge-147/abigail/java/ch-1.java @@ -0,0 +1,54 @@ +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 +// + +import java.util.*; +import java.util.ArrayList; + +public class ch1 { + public static boolean is_prime (int p) { + if (p == 2) {return true;} + if (p % 2 == 0) {return false;} + for (int i = 3; i * i <= p; i += 2) { + if (p % i == 0) {return false;} + } + return (true); + } + + public static void main (String [] args) { + ArrayList todo = new ArrayList (); + todo . add (2); + todo . add (3); + todo . add (5); + todo . add (7); + for (int p: todo) { + System . out . printf ("%d ", p); + } + int count = 20 - todo . size (); + int pow = 10; + main: + while (todo . size () > 0) { + ArrayList new_todo = new ArrayList (); + for (int d = 1; d <= 9; d ++) { + for (int p: todo) { + int candidate = d * pow + p; + if (is_prime (candidate)) { + System . out . printf ("%d ", candidate); + new_todo . add (candidate); + count --; + if (count <= 0) { + break main; + } + } + } + } + pow = pow * 10; + todo = new_todo; + } + System . out . print ("\n"); + } +} diff --git a/challenge-147/abigail/java/ch-2.java b/challenge-147/abigail/java/ch-2.java new file mode 100644 index 0000000000..5469775b97 --- /dev/null +++ b/challenge-147/abigail/java/ch-2.java @@ -0,0 +1,32 @@ +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 +// + +import java.util.*; + +public class ch2 { + public static void main (String [] args) { + int p = 0; + int n = 0; + Map pentagon = new Hashtable (); + boolean done = false; + while (true) { + p += n + n + n + 1; + n ++; + pentagon . put (p, true); + for (Map . Entry entry: pentagon . entrySet ()) { + Integer seen = entry . getKey (); + if (seen + seen < p && + pentagon . containsKey (p - seen) && + pentagon . containsKey (p - seen - seen)) { + System . out . printf ("%d %d\n", seen, p - seen); + return; + } + } + } + } +} -- cgit From 0b93e274674688f3ae6702e168b0e0391c4a5bf5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 15 Jan 2022 19:47:06 +0100 Subject: Week 147: Lua solutions --- challenge-147/abigail/lua/ch-1.lua | 57 ++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/lua/ch-2.lua | 29 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 challenge-147/abigail/lua/ch-1.lua create mode 100644 challenge-147/abigail/lua/ch-2.lua diff --git a/challenge-147/abigail/lua/ch-1.lua b/challenge-147/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..0c86152ba1 --- /dev/null +++ b/challenge-147/abigail/lua/ch-1.lua @@ -0,0 +1,57 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +-- + +-- +-- Run as: lua ch-1.lua +-- + +function is_prime (p) + if p == 2 then + return true + end + if p % 2 == 0 then + return false + end + i = 3 + while i * i <= p do + if p % i == 0 then + return false + end + i = i + 2 + end + return true +end + +todo = {2, 3, 5, 7} +for i, p in ipairs (todo) do + io . write (p, " ") +end + +count = 20 - #todo + +pow = 10 +while #todo > 0 do + new_todo = {} + for d = 1, 9 do + for i, p in ipairs (todo) do + candidate = d * pow + p + if is_prime (candidate) then + io . write (candidate, " ") + new_todo [#new_todo + 1] = candidate + count = count - 1 + if count <= 0 then + goto end_of_while + end + end + end + end + todo = new_todo + pow = pow * 10 +end + +::end_of_while:: + +io . write ("\n") diff --git a/challenge-147/abigail/lua/ch-2.lua b/challenge-147/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..1d93585f24 --- /dev/null +++ b/challenge-147/abigail/lua/ch-2.lua @@ -0,0 +1,29 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +-- + +-- +-- Run as: lua ch-2.lua +-- + +pentagon = {} +p = 0 +n = 0 + +while true do + p = p + n + n + n + 1 + n = n + 1 + pentagon [p] = 1 + for seen in pairs (pentagon) do + if seen + seen <= p and pentagon [p - seen] ~= nil + and pentagon [p - seen - seen] ~= nil then + print (seen .. " " .. (p - seen)) + goto end_of_while + end + end +end + +::end_of_while:: + -- cgit From 3675e785bd1ddadcff97eb6725e1466eb1738f7a Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 15 Jan 2022 19:59:11 +0100 Subject: Week 147: Node.js solutions --- challenge-147/abigail/node/ch-1.js | 47 ++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/node/ch-2.js | 29 +++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 challenge-147/abigail/node/ch-1.js create mode 100644 challenge-147/abigail/node/ch-2.js diff --git a/challenge-147/abigail/node/ch-1.js b/challenge-147/abigail/node/ch-1.js new file mode 100644 index 0000000000..2a6c5c3764 --- /dev/null +++ b/challenge-147/abigail/node/ch-1.js @@ -0,0 +1,47 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +// + +// +// Run as: node ch-1.js +// + +function is_prime (p) { + if (p == 2) {return true} + if (p % 2 == 0) {return false} + for (let i = 3; i * i <= p; i += 2) { + if (p % i == 0) {return false} + } + return true +} + +let todo = [2, 3, 5, 7] +let count = 20 - todo . length + +todo . forEach (p => process . stdout . write (p . toString () + " ")) + +let pow = 10 +main: { + while (todo . length > 0) { + let new_todo = [] + for (let d = 1; d <= 9; d ++) { + for (let i = 0; i < todo . length; i ++) { + let candidate = d * pow + todo [i] + if (is_prime (candidate)) { + new_todo . push (candidate) + process . stdout . write (candidate . toString () + " ") + count -- + if (count <= 0) { + break main + } + } + } + } + todo = new_todo + pow *= 10 + } +} + +process . stdout . write ("\n") diff --git a/challenge-147/abigail/node/ch-2.js b/challenge-147/abigail/node/ch-2.js new file mode 100644 index 0000000000..e06832ee9b --- /dev/null +++ b/challenge-147/abigail/node/ch-2.js @@ -0,0 +1,29 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +// + +// +// Run as: node ch-2.js +// + +let pentagon = {} +let p = 0 +let n = 0 + +main: { + while (1) { + p += n + n + n + 1 + n ++ + pentagon [p] = 1 + for (let seen in pentagon) { + seen = +seen + if (seen + seen <= p && (p - seen) in pentagon && + (p - seen - seen) in pentagon) { + console . log (seen . toString () + " " + (p - seen)) + break main + } + } + } +} -- cgit From 229529902e59d78ca79aebf2e8a42ef1382cfa1d Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 15 Jan 2022 22:28:24 +0100 Subject: Week 147: Pascal solutions --- challenge-147/abigail/pascal/ch-1.p | 82 +++++++++++++++++++++++++++++++++++++ challenge-147/abigail/pascal/ch-2.p | 41 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 challenge-147/abigail/pascal/ch-1.p create mode 100644 challenge-147/abigail/pascal/ch-2.p diff --git a/challenge-147/abigail/pascal/ch-1.p b/challenge-147/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..24dc514652 --- /dev/null +++ b/challenge-147/abigail/pascal/ch-1.p @@ -0,0 +1,82 @@ +Program XXX; + +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out *) +(* *) + +var + todo: array of integer; + new_todo: array of integer; + pow, i, d: integer; + count: integer = 20; + candidate: integer; + +function is_prime (p: integer): boolean; + var + i: integer; + + begin + if p = 2 then begin + is_prime := true; + exit; + end; + if p mod 2 = 0 then begin + is_prime := false; + exit; + end; + i := 3; + while i * i <= p do begin + if p mod i = 0 then begin + is_prime := false; + exit; + end; + i := i + 2; + end; + is_prime := true; + exit; + end; + + +begin + setlength (todo, 4); + todo [0] := 2; + todo [1] := 3; + todo [2] := 5; + todo [3] := 7; + for i := 0 to 3 do begin + write (todo [i], ' '); + end; + count := count - 4; + pow := 10; + while count > 0 do begin + setlength (new_todo, 0); + for d := 1 to 9 do begin + for i := 0 to length (todo) - 1 do begin + candidate := d * pow + todo [i]; + if is_prime (candidate) then begin + insert (candidate, new_todo, length (new_todo)); + write (candidate, ' '); + count := count - 1; + if count <= 0 then begin + break; + end; + end; + if count <= 0 then begin + break; + end; + end; + if count <= 0 then begin + break; + end; + end; + todo := new_todo; + pow := pow * 10; + end; + writeln (''); +end. + +end. diff --git a/challenge-147/abigail/pascal/ch-2.p b/challenge-147/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..ac8d08d076 --- /dev/null +++ b/challenge-147/abigail/pascal/ch-2.p @@ -0,0 +1,41 @@ +Program ch2; + +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out *) +(* *) + +uses + fgl; + +var + pentagon: specialize TFPGMap ; + n, p, seen, i: longint; + done: boolean; + +begin + pentagon := specialize TFPGMap . Create; + p := 0; + n := 0; + done := false; + while not done do begin + p := p + n + n + n + 1; + n := n + 1; + pentagon . Add (p, true); + for i := 0 to pentagon . count - 1 do begin + seen := pentagon . Keys [i]; + if (seen + seen <= p) and + (pentagon . IndexOf (p - seen) >= 0) and + (pentagon . IndexOf (p - seen - seen) >= 0) then begin + writeln (seen, ' ', p - seen); + done := true; + end; + if done then begin + break; + end; + end + end +end. -- cgit From 4b1c4fc7e1809c724d3d994f0de6d97c4a14b415 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 13:28:01 +0100 Subject: Week 147: Python solutions --- challenge-147/abigail/python/ch-1.py | 37 ++++++++++++++++++++++++++++++++++++ challenge-147/abigail/python/ch-2.py | 29 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 challenge-147/abigail/python/ch-1.py create mode 100644 challenge-147/abigail/python/ch-2.py diff --git a/challenge-147/abigail/python/ch-1.py b/challenge-147/abigail/python/ch-1.py new file mode 100644 index 0000000000..cd317b880b --- /dev/null +++ b/challenge-147/abigail/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: python ch-1.py +# + +import sys +from sympy import isprime + +todo = [2, 3, 5, 7] +[sys . stdout . write (str (x) + " ") for x in todo] +count = 20 - len (todo) + +pow = 10 +while count > 0: + new_todo = [] + for d in range (1, 10): + for p in todo: + candidate = d * pow + p + if isprime (candidate): + sys . stdout . write (str (candidate) + " ") + count = count - 1 + new_todo . append (candidate) + if count <= 0: + break + if count <= 0: + break + if count <= 0: + break + todo = new_todo + pow = pow * 10 + +sys . stdout . write ("\n") diff --git a/challenge-147/abigail/python/ch-2.py b/challenge-147/abigail/python/ch-2.py new file mode 100644 index 0000000000..9d8ae317b3 --- /dev/null +++ b/challenge-147/abigail/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: python ch-2.py +# + +pentagon = {} +p = 0 +n = 0 +done = False + + +while not done: + p = p + n + n + n + 1 + n = n + 1 + pentagon [p] = True + + for seen in pentagon: + if seen + seen <= p and (p - seen) in pentagon \ + and (p - seen - seen) in pentagon: + print (seen, p - seen) + done = True + break + if done: + break -- cgit From 6a42ef0f1bfd7b33cd271aff9a0323cf97620bad Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 17:42:00 +0100 Subject: Week 147: R solutions --- challenge-147/abigail/r/ch-1.r | 46 ++++++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/r/ch-2.r | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-147/abigail/r/ch-1.r create mode 100644 challenge-147/abigail/r/ch-2.r diff --git a/challenge-147/abigail/r/ch-1.r b/challenge-147/abigail/r/ch-1.r new file mode 100644 index 0000000000..ef6dddc678 --- /dev/null +++ b/challenge-147/abigail/r/ch-1.r @@ -0,0 +1,46 @@ +#!/usr/local/bin/Rscript + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: Rscript ch-1.r +# + +suppressPackageStartupMessages ( + library (gmp) +) + +todo = c (2, 3, 5, 7) +cat (todo, "") + +count = 20 - length (todo) + +pow = 10 +while (count > 0 && length (todo) > 0) { + new_todo = c () + for (d in 1 : 9) { + for (p in todo) { + candidate = d * pow + p + if (isprime (candidate) > 0) { + cat (candidate, "") + count = count - 1 + new_todo = c (new_todo, candidate) + if (count <= 0) { + break + } + } + if (count <= 0) { + break + } + } + if (count <= 0) { + break + } + } + pow = pow * 10 + todo = new_todo +} + +cat ("\n") diff --git a/challenge-147/abigail/r/ch-2.r b/challenge-147/abigail/r/ch-2.r new file mode 100644 index 0000000000..5ae1bf82e5 --- /dev/null +++ b/challenge-147/abigail/r/ch-2.r @@ -0,0 +1,37 @@ +#!/usr/local/bin/Rscript + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: Rscript ch-2.r +# + +suppressPackageStartupMessages ( + library (hash) +) + +pentagon <- hash () +p <- 0 +n <- 0 +done <- FALSE + +while (!done) { + p <- p + n + n + n + 1 + n <- n + 1 + .set (pentagon, p, 1) + + for (seen in keys (pentagon)) { + seen <- as.numeric (seen) + if (seen + seen <= p && + all (has.key (c (as.character (p - seen), + as.character (p - seen - seen)), pentagon))) { + cat (seen, p - seen, "\n") + done = TRUE + } + if (done) { + break + } + } +} -- cgit From 0cbed3d1cd8b789156d383d62f25a2ebaff00371 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 19:03:20 +0100 Subject: Week 147: Ruby solutions --- challenge-147/abigail/ruby/ch-1.rb | 49 ++++++++++++++++++++++++++++++++++++++ challenge-147/abigail/ruby/ch-2.rb | 30 +++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 challenge-147/abigail/ruby/ch-1.rb create mode 100644 challenge-147/abigail/ruby/ch-2.rb diff --git a/challenge-147/abigail/ruby/ch-1.rb b/challenge-147/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..0e0eaffc4b --- /dev/null +++ b/challenge-147/abigail/ruby/ch-1.rb @@ -0,0 +1,49 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: ruby ch-1.rb +# + +require 'prime' + +todo = [2, 3, 5, 7]; + +todo . each do + | x | + print (x . to_s + " ") +end + +count = 20 - todo . length + +pow = 10 +while todo . length > 0 && count > 0 do + new_todo = [] + for d in 1 .. 9 do + todo . each do + | p | + candidate = d * pow + p + if (Prime . prime? (candidate)) then + new_todo . push (candidate) + print (candidate . to_s + " ") + count = count - 1 + if (count <= 0) then + break + end + end + if (count <= 0) then + break + end + end + if (count <= 0) then + break + end + end + pow = pow * 10 + todo = new_todo +end + +puts ("") diff --git a/challenge-147/abigail/ruby/ch-2.rb b/challenge-147/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..a3dbb601cc --- /dev/null +++ b/challenge-147/abigail/ruby/ch-2.rb @@ -0,0 +1,30 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: ruby ch-2.rb +# + +pentagon = {} +p = 0 +n = 0 +done = false + + +while not done do + p = p + n + n + n + 1 + n = n + 1 + pentagon [p] = true + pentagon . each do + | seen, _ | + if (seen + seen <= p && pentagon . key?(p - seen) \ + && pentagon . key?(p - seen - seen)) then + puts (seen . to_s + " " + (p - seen) . to_s) + done = true + break + end + end +end -- cgit From 4d99862bf4e64b7a33d5c5bc52a4b9b5b9ab109d Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 19:31:17 +0100 Subject: Week 147: Hello, World solutions. --- challenge-147/abigail/basic/ch-1.bas | 9 +++++++++ challenge-147/abigail/befunge-93/ch-1.bf93 | 2 ++ challenge-147/abigail/cobol/ch-1.cb | 14 ++++++++++++++ challenge-147/abigail/csh/ch-1.csh | 11 +++++++++++ challenge-147/abigail/erlang/ch-1.erl | 15 +++++++++++++++ challenge-147/abigail/forth/ch-1.fs | 5 +++++ challenge-147/abigail/fortran/ch-1.f90 | 12 ++++++++++++ challenge-147/abigail/m4/ch-1.m4 | 1 + challenge-147/abigail/mmix/ch-1.mms | 17 +++++++++++++++++ challenge-147/abigail/ocaml/ch-1.ml | 9 +++++++++ challenge-147/abigail/php/ch-1.php | 11 +++++++++++ challenge-147/abigail/postscript/ch-1.ps | 10 ++++++++++ challenge-147/abigail/rexx/ch-1.rexx | 9 +++++++++ challenge-147/abigail/scheme/ch-1.scm | 12 ++++++++++++ challenge-147/abigail/sed/ch-1.sed | 12 ++++++++++++ challenge-147/abigail/sql/ch-1.sql | 9 +++++++++ challenge-147/abigail/t/input-1-1 | 1 + challenge-147/abigail/tcl/ch-1.tcl | 11 +++++++++++ 18 files changed, 170 insertions(+) create mode 100644 challenge-147/abigail/basic/ch-1.bas create mode 100644 challenge-147/abigail/befunge-93/ch-1.bf93 create mode 100644 challenge-147/abigail/cobol/ch-1.cb create mode 100644 challenge-147/abigail/csh/ch-1.csh create mode 100644 challenge-147/abigail/erlang/ch-1.erl create mode 100644 challenge-147/abigail/forth/ch-1.fs create mode 100644 challenge-147/abigail/fortran/ch-1.f90 create mode 100644 challenge-147/abigail/m4/ch-1.m4 create mode 100644 challenge-147/abigail/mmix/ch-1.mms create mode 100644 challenge-147/abigail/ocaml/ch-1.ml create mode 100644 challenge-147/abigail/php/ch-1.php create mode 100644 challenge-147/abigail/postscript/ch-1.ps create mode 100644 challenge-147/abigail/rexx/ch-1.rexx create mode 100644 challenge-147/abigail/scheme/ch-1.scm create mode 100644 challenge-147/abigail/sed/ch-1.sed create mode 100644 challenge-147/abigail/sql/ch-1.sql create mode 100644 challenge-147/abigail/tcl/ch-1.tcl diff --git a/challenge-147/abigail/basic/ch-1.bas b/challenge-147/abigail/basic/ch-1.bas new file mode 100644 index 0000000000..d8dd4cc894 --- /dev/null +++ b/challenge-147/abigail/basic/ch-1.bas @@ -0,0 +1,9 @@ +010 REM +020 REM See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +030 REM + +040 REM +050 REM Run as: basic ch-1.bas +060 REM + +100 PRINT "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197" \ No newline at end of file diff --git a/challenge-147/abigail/befunge-93/ch-1.bf93 b/challenge-147/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..1125f4707a --- /dev/null +++ b/challenge-147/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,2 @@ +< v,_@#:< "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197" +55 + > ^ \ No newline at end of file diff --git a/challenge-147/abigail/cobol/ch-1.cb b/challenge-147/abigail/cobol/ch-1.cb new file mode 100644 index 0000000000..c44aa26483 --- /dev/null +++ b/challenge-147/abigail/cobol/ch-1.cb @@ -0,0 +1,14 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. XXX. + +*> +*> See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +*> + +*> +*> Run as: cobc -xF -o ch-1.o ch-1.cb; ./ch-1.o +*> + +PROCEDURE DIVISION. + DISPLAY "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197". + STOP RUN. diff --git a/challenge-147/abigail/csh/ch-1.csh b/challenge-147/abigail/csh/ch-1.csh new file mode 100644 index 0000000000..c0c728af68 --- /dev/null +++ b/challenge-147/abigail/csh/ch-1.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: csh ch-1.csh +# + +echo "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197" \ No newline at end of file diff --git a/challenge-147/abigail/erlang/ch-1.erl b/challenge-147/abigail/erlang/ch-1.erl new file mode 100644 index 0000000000..901922151e --- /dev/null +++ b/challenge-147/abigail/erlang/ch-1.erl @@ -0,0 +1,15 @@ +% +% See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +% + +% +% 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 ("2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197\n"). \ No newline at end of file diff --git a/challenge-147/abigail/forth/ch-1.fs b/challenge-147/abigail/forth/ch-1.fs new file mode 100644 index 0000000000..22bd24e25a --- /dev/null +++ b/challenge-147/abigail/forth/ch-1.fs @@ -0,0 +1,5 @@ +\ +\ See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +\ + +.( 2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197) \ No newline at end of file diff --git a/challenge-147/abigail/fortran/ch-1.f90 b/challenge-147/abigail/fortran/ch-1.f90 new file mode 100644 index 0000000000..afa50a9bc8 --- /dev/null +++ b/challenge-147/abigail/fortran/ch-1.f90 @@ -0,0 +1,12 @@ +! +! See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +! + +! +! Run as: gfortran -o ch-1.o ch-1.f90; ./ch-1.o +! + +program ch1 + implicit none + write (*, *) "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197" +end \ No newline at end of file diff --git a/challenge-147/abigail/m4/ch-1.m4 b/challenge-147/abigail/m4/ch-1.m4 new file mode 100644 index 0000000000..3648c94b73 --- /dev/null +++ b/challenge-147/abigail/m4/ch-1.m4 @@ -0,0 +1 @@ +2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197 diff --git a/challenge-147/abigail/mmix/ch-1.mms b/challenge-147/abigail/mmix/ch-1.mms new file mode 100644 index 0000000000..8024fd6ceb --- /dev/null +++ b/challenge-147/abigail/mmix/ch-1.mms @@ -0,0 +1,17 @@ +% +% See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +% + +% +% Run as: mmixal -o ch-1.mmo ch-1.mms; mmix -q ch-1.mmo +% + LOC Data_Segment + GREG @ +Text BYTE "2 3 5 7 13 17 23 37 43 47 53 67 73 83 " + BYTE "97 113 137 167 173 197",10,0 + + LOC #100 + +Main LDA $255,Text + TRAP 0,Fputs,StdOut + TRAP 0,Halt,0 \ No newline at end of file diff --git a/challenge-147/abigail/ocaml/ch-1.ml b/challenge-147/abigail/ocaml/ch-1.ml new file mode 100644 index 0000000000..700d19281c --- /dev/null +++ b/challenge-147/abigail/ocaml/ch-1.ml @@ -0,0 +1,9 @@ +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 *) +(* *) + +(* *) +(* Run as: ocaml ch-1.ml *) +(* *) + +print_endline "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197"; \ No newline at end of file diff --git a/challenge-147/abigail/php/ch-1.php b/challenge-147/abigail/php/ch-1.php new file mode 100644 index 0000000000..10f463c368 --- /dev/null +++ b/challenge-147/abigail/php/ch-1.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/challenge-147/abigail/postscript/ch-1.ps b/challenge-147/abigail/postscript/ch-1.ps new file mode 100644 index 0000000000..ea35a6dc64 --- /dev/null +++ b/challenge-147/abigail/postscript/ch-1.ps @@ -0,0 +1,10 @@ +%!PS +% +% See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +% + +% +% Run as: ps2ascii ch-1.ps +% + +(2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197) = \ No newline at end of file diff --git a/challenge-147/abigail/rexx/ch-1.rexx b/challenge-147/abigail/rexx/ch-1.rexx new file mode 100644 index 0000000000..8fca035ac4 --- /dev/null +++ b/challenge-147/abigail/rexx/ch-1.rexx @@ -0,0 +1,9 @@ +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + */ + +/* + * Run as: rexx ch-1.rexx + */ + +say "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197" \ No newline at end of file diff --git a/challenge-147/abigail/scheme/ch-1.scm b/challenge-147/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..34c159b213 --- /dev/null +++ b/challenge-147/abigail/scheme/ch-1.scm @@ -0,0 +1,12 @@ +#!/usr/local/bin/guile +!# + +;;; +;;; See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + +(display "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197\n") \ No newline at end of file diff --git a/challenge-147/abigail/sed/ch-1.sed b/challenge-147/abigail/sed/ch-1.sed new file mode 100644 index 0000000000..a9564d943e --- /dev/null +++ b/challenge-147/abigail/sed/ch-1.sed @@ -0,0 +1,12 @@ +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: sed -f ch-1.sed +# +# For each line in the input file, we write the first three +# self-describing numbers. +# + +s/.*/2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197/ \ No newline at end of file diff --git a/challenge-147/abigail/sql/ch-1.sql b/challenge-147/abigail/sql/ch-1.sql new file mode 100644 index 0000000000..fddc84ac20 --- /dev/null +++ b/challenge-147/abigail/sql/ch-1.sql @@ -0,0 +1,9 @@ +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +-- + +-- +-- Run as: sqlite3 < ch-1.sql +-- + +SELECT "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197"; \ No newline at end of file diff --git a/challenge-147/abigail/t/input-1-1 b/challenge-147/abigail/t/input-1-1 index e69de29bb2..8d1c8b69c3 100644 --- a/challenge-147/abigail/t/input-1-1 +++ b/challenge-147/abigail/t/input-1-1 @@ -0,0 +1 @@ + diff --git a/challenge-147/abigail/tcl/ch-1.tcl b/challenge-147/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..d61e54d57b --- /dev/null +++ b/challenge-147/abigail/tcl/ch-1.tcl @@ -0,0 +1,11 @@ +#!/usr/local/opt/tcl-tk/bin/tclsh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: tclsh ch-1.tcl +# + +puts "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197" \ No newline at end of file -- cgit From 6476972d82647f40e1c68ec2d6434ba05147a7bb Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 19:39:48 +0100 Subject: Week 147: Hello, world solutions --- challenge-147/abigail/basic/ch-2.bas | 9 +++++++++ challenge-147/abigail/befunge-93/ch-2.bf93 | 2 ++ challenge-147/abigail/cobol/ch-2.cb | 14 ++++++++++++++ challenge-147/abigail/csh/ch-2.csh | 11 +++++++++++ challenge-147/abigail/erlang/ch-2.erl | 15 +++++++++++++++ challenge-147/abigail/forth/ch-2.fs | 5 +++++ challenge-147/abigail/fortran/ch-2.f90 | 12 ++++++++++++ challenge-147/abigail/m4/ch-2.m4 | 1 + challenge-147/abigail/mmix/ch-2.mms | 16 ++++++++++++++++ challenge-147/abigail/ocaml/ch-2.ml | 9 +++++++++ challenge-147/abigail/php/ch-2.php | 11 +++++++++++ challenge-147/abigail/postscript/ch-2.ps | 10 ++++++++++ challenge-147/abigail/rexx/ch-2.rexx | 9 +++++++++ challenge-147/abigail/scheme/ch-2.scm | 12 ++++++++++++ challenge-147/abigail/sed/ch-2.sed | 12 ++++++++++++ challenge-147/abigail/sql/ch-2.sql | 9 +++++++++ challenge-147/abigail/t/input-2-1 | 1 + challenge-147/abigail/tcl/ch-2.tcl | 11 +++++++++++ 18 files changed, 169 insertions(+) create mode 100644 challenge-147/abigail/basic/ch-2.bas create mode 100644 challenge-147/abigail/befunge-93/ch-2.bf93 create mode 100644 challenge-147/abigail/cobol/ch-2.cb create mode 100644 challenge-147/abigail/csh/ch-2.csh create mode 100644 challenge-147/abigail/erlang/ch-2.erl create mode 100644 challenge-147/abigail/forth/ch-2.fs create mode 100644 challenge-147/abigail/fortran/ch-2.f90 create mode 100644 challenge-147/abigail/m4/ch-2.m4 create mode 100644 challenge-147/abigail/mmix/ch-2.mms create mode 100644 challenge-147/abigail/ocaml/ch-2.ml create mode 100644 challenge-147/abigail/php/ch-2.php create mode 100644 challenge-147/abigail/postscript/ch-2.ps create mode 100644 challenge-147/abigail/rexx/ch-2.rexx create mode 100644 challenge-147/abigail/scheme/ch-2.scm create mode 100644 challenge-147/abigail/sed/ch-2.sed create mode 100644 challenge-147/abigail/sql/ch-2.sql create mode 100644 challenge-147/abigail/tcl/ch-2.tcl diff --git a/challenge-147/abigail/basic/ch-2.bas b/challenge-147/abigail/basic/ch-2.bas new file mode 100644 index 0000000000..b4b850e9b6 --- /dev/null +++ b/challenge-147/abigail/basic/ch-2.bas @@ -0,0 +1,9 @@ +010 REM +020 REM See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +030 REM + +040 REM +050 REM Run as: basic ch-2.bas +060 REM + +100 PRINT "1560090 7042750" diff --git a/challenge-147/abigail/befunge-93/ch-2.bf93 b/challenge-147/abigail/befunge-93/ch-2.bf93 new file mode 100644 index 0000000000..1b6bdf5f9b --- /dev/null +++ b/challenge-147/abigail/befunge-93/ch-2.bf93 @@ -0,0 +1,2 @@ +< v,_@#:< "1560090 7042750" +55 + > ^ diff --git a/challenge-147/abigail/cobol/ch-2.cb b/challenge-147/abigail/cobol/ch-2.cb new file mode 100644 index 0000000000..741375f6f2 --- /dev/null +++ b/challenge-147/abigail/cobol/ch-2.cb @@ -0,0 +1,14 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. XXX. + +*> +*> See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +*> + +*> +*> Run as: cobc -xF -o ch-2.o ch-2.cb; ./ch-2.o +*> + +PROCEDURE DIVISION. + DISPLAY "1560090 7042750". + STOP RUN. diff --git a/challenge-147/abigail/csh/ch-2.csh b/challenge-147/abigail/csh/ch-2.csh new file mode 100644 index 0000000000..14b13435d2 --- /dev/null +++ b/challenge-147/abigail/csh/ch-2.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: csh ch-2.csh +# + +echo "1560090 7042750" diff --git a/challenge-147/abigail/erlang/ch-2.erl b/challenge-147/abigail/erlang/ch-2.erl new file mode 100644 index 0000000000..1894167a54 --- /dev/null +++ b/challenge-147/abigail/erlang/ch-2.erl @@ -0,0 +1,15 @@ +% +% See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +% + +% +% Run as: ln ch-2.erl ch2.erl +% erl -compile ch2 +% erl -noshell -s ch2 main -s init stop +% + +-module (ch2). +-export ([main/0]). + +main () -> + io:fwrite ("1560090 7042750\n"). diff --git a/challenge-147/abigail/forth/ch-2.fs b/challenge-147/abigail/forth/ch-2.fs new file mode 100644 index 0000000000..5d91bd27a8 --- /dev/null +++ b/challenge-147/abigail/forth/ch-2.fs @@ -0,0 +1,5 @@ +\ +\ See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +\ + +.( 1560090 7042750) diff --git a/challenge-147/abigail/fortran/ch-2.f90 b/challenge-147/abigail/fortran/ch-2.f90 new file mode 100644 index 0000000000..481b4458a7 --- /dev/null +++ b/challenge-147/abigail/fortran/ch-2.f90 @@ -0,0 +1,12 @@ +! +! See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +! + +! +! Run as: gfortran -o ch-2.o ch-2.f90; ./ch-2.o +! + +program ch2 + implicit none + write (*, *) "1560090 7042750" +end diff --git a/challenge-147/abigail/m4/ch-2.m4 b/challenge-147/abigail/m4/ch-2.m4 new file mode 100644 index 0000000000..de3d6ccd8a --- /dev/null +++ b/challenge-147/abigail/m4/ch-2.m4 @@ -0,0 +1 @@ +1560090 7042750 diff --git a/challenge-147/abigail/mmix/ch-2.mms b/challenge-147/abigail/mmix/ch-2.mms new file mode 100644 index 0000000000..3c06a5424f --- /dev/null +++ b/challenge-147/abigail/mmix/ch-2.mms @@ -0,0 +1,16 @@ +% +% See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +% + +% +% Run as: mmixal -o ch-2.mmo ch-2.mms; mmix -q ch-2.mmo +% + LOC Data_Segment + GREG @ +Text BYTE "1560090 7042750",10,0 + + LOC #100 + +Main LDA $255,Text + TRAP 0,Fputs,StdOut + TRAP 0,Halt,0 diff --git a/challenge-147/abigail/ocaml/ch-2.ml b/challenge-147/abigail/ocaml/ch-2.ml new file mode 100644 index 0000000000..a9ceff2496 --- /dev/null +++ b/challenge-147/abigail/ocaml/ch-2.ml @@ -0,0 +1,9 @@ +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 *) +(* *) + +(* *) +(* Run as: ocaml ch-2.ml *) +(* *) + +print_endline "1560090 7042750"; diff --git a/challenge-147/abigail/php/ch-2.php b/challenge-147/abigail/php/ch-2.php new file mode 100644 index 0000000000..0b4fc1567c --- /dev/null +++ b/challenge-147/abigail/php/ch-2.php @@ -0,0 +1,11 @@ + diff --git a/challenge-147/abigail/postscript/ch-2.ps b/challenge-147/abigail/postscript/ch-2.ps new file mode 100644 index 0000000000..177894975c --- /dev/null +++ b/challenge-147/abigail/postscript/ch-2.ps @@ -0,0 +1,10 @@ +%!PS +% +% See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +% + +% +% Run as: ps2ascii ch-2.ps +% + +(1560090 7042750) = diff --git a/challenge-147/abigail/rexx/ch-2.rexx b/challenge-147/abigail/rexx/ch-2.rexx new file mode 100644 index 0000000000..bf0cdb5223 --- /dev/null +++ b/challenge-147/abigail/rexx/ch-2.rexx @@ -0,0 +1,9 @@ +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + */ + +/* + * Run as: rexx ch-2.rexx + */ + +say "1560090 7042750" diff --git a/challenge-147/abigail/scheme/ch-2.scm b/challenge-147/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..81bdeb1cdd --- /dev/null +++ b/challenge-147/abigail/scheme/ch-2.scm @@ -0,0 +1,12 @@ +#!/usr/local/bin/guile +!# + +;;; +;;; See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm +;;; + +(display "1560090 7042750\n") diff --git a/challenge-147/abigail/sed/ch-2.sed b/challenge-147/abigail/sed/ch-2.sed new file mode 100644 index 0000000000..bf37937d38 --- /dev/null +++ b/challenge-147/abigail/sed/ch-2.sed @@ -0,0 +1,12 @@ +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: sed -f ch-2.sed +# +# For each line in the input file, we write the first three +# self-describing numbers. +# + +s/.*/1560090 7042750/ diff --git a/challenge-147/abigail/sql/ch-2.sql b/challenge-147/abigail/sql/ch-2.sql new file mode 100644 index 0000000000..ef0a3578ac --- /dev/null +++ b/challenge-147/abigail/sql/ch-2.sql @@ -0,0 +1,9 @@ +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +-- + +-- +-- Run as: sqlite3 < ch-2.sql +-- + +SELECT "1560090 7042750"; diff --git a/challenge-147/abigail/t/input-2-1 b/challenge-147/abigail/t/input-2-1 index e69de29bb2..8d1c8b69c3 100644 --- a/challenge-147/abigail/t/input-2-1 +++ b/challenge-147/abigail/t/input-2-1 @@ -0,0 +1 @@ + diff --git a/challenge-147/abigail/tcl/ch-2.tcl b/challenge-147/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..6565977dd1 --- /dev/null +++ b/challenge-147/abigail/tcl/ch-2.tcl @@ -0,0 +1,11 @@ +#!/usr/local/opt/tcl-tk/bin/tclsh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: tclsh ch-2.tcl +# + +puts "1560090 7042750" -- cgit From be7dfc6724bad12f4e9dae9c321a1090141b41fe Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 19:41:32 +0100 Subject: Week 147: README --- challenge-147/abigail/README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/challenge-147/abigail/README.md b/challenge-147/abigail/README.md index f819e6f82c..7087e5bc74 100644 --- a/challenge-147/abigail/README.md +++ b/challenge-147/abigail/README.md @@ -37,16 +37,31 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [bc](bc/ch-2.bc) +* [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](lua/ch-2.erl) +* [Forth](lua/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) +* [MMIX](mmix/ch-2.mms) * [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) -* [Tcl](tcl/ch-2.tcl) * [Scheme](scheme/ch-2.scm) +* [Sed](sed/ch-2.sed) +* [SQL](sql/ch-2.sql) +* [Tcl](tcl/ch-2.tcl) -- cgit From 1009553cceb45eb905ca454598980d33ca35ee7b Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 21:39:45 +0100 Subject: Week 147: remove outcommented code --- challenge-147/abigail/bash/ch-2.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/challenge-147/abigail/bash/ch-2.sh b/challenge-147/abigail/bash/ch-2.sh index f38223377d..ad63335bba 100644 --- a/challenge-147/abigail/bash/ch-2.sh +++ b/challenge-147/abigail/bash/ch-2.sh @@ -12,9 +12,6 @@ set -f declare -A pentagon -# ((n = 0)) -# ((p = 0)) - while true do ((p = p + n + n + n + 1)) ((n ++)) -- cgit From 47b1be11f603186d1c9702fe509e9bed557967b3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 16 Jan 2022 22:57:51 +0100 Subject: Week 147: Better Tcl solutions --- challenge-147/abigail/tcl/ch-1.tcl | 43 +++++++++++++++++++++++++++++++++++++- challenge-147/abigail/tcl/ch-2.tcl | 22 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/challenge-147/abigail/tcl/ch-1.tcl b/challenge-147/abigail/tcl/ch-1.tcl index d61e54d57b..22ff96b19c 100644 --- a/challenge-147/abigail/tcl/ch-1.tcl +++ b/challenge-147/abigail/tcl/ch-1.tcl @@ -8,4 +8,45 @@ # Run as: tclsh ch-1.tcl # -puts "2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197" \ No newline at end of file +proc is_prime n { + if {$n == 2} {return 1} + if {$n % 2 == 0} {return 0} + for {set i 3} {$i * $i <= $n} {incr i 2} { + if {$n % $i == 0} {return 0} + } + return 1 +} + +set todo {2 3 5 7} +set count [expr 20 - [llength $todo]] + +puts -nonewline $todo +puts -nonewline " " + +while {[llength $todo] > 0} { + set new_todo {} + foreach d [split 123456789 ""] { + foreach p $todo { + set candidate $d$p + if {[is_prime $candidate]} { + puts -nonewline $candidate + puts -nonewline " " + incr count -1 + lappend new_todo $candidate + if {$count <= 0} { + break + } + } + if {$count <= 0} { + break +