diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2022-01-24 22:05:59 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2022-01-24 22:05:59 +0800 |
| commit | 1ff224c623b04c549b24ec402ea43131cd910588 (patch) | |
| tree | d5b82082d798443119bb481f34bfdb0c4d49863f /challenge-147 | |
| parent | 59e30e384e1e158ad9da7494efdb4cb810c75c88 (diff) | |
| parent | 58fe0dd310fd5641512650fa783103ddbb5fc384 (diff) | |
| download | perlweeklychallenge-club-1ff224c623b04c549b24ec402ea43131cd910588.tar.gz perlweeklychallenge-club-1ff224c623b04c549b24ec402ea43131cd910588.tar.bz2 perlweeklychallenge-club-1ff224c623b04c549b24ec402ea43131cd910588.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-147')
74 files changed, 1650 insertions, 7 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) 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 + } + } + } +} + 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..ad63335bba --- /dev/null +++ b/challenge-147/abigail/bash/ch-2.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: bash ch-2.sh +# + +set -f + +declare -A pentagon + +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 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/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/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 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/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/blog.txt b/challenge-147/abigail/blog.txt new file mode 100644 index 0000000000..78a697713d --- /dev/null +++ b/challenge-147/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-147-1.html diff --git a/challenge-147/abigail/blog1.txt b/challenge-147/abigail/blog1.txt new file mode 100644 index 0000000000..2f375ee37a --- /dev/null +++ b/challenge-147/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-147-2.html 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 <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <stdbool.h> + +/* + * 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 <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <stdbool.h> + +/* + * 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; |
