From 8254e0cdd38e99a76e12136123400a6160d6f160 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 20 Dec 2021 18:30:29 +0100 Subject: Solutions for week 144, part 1 This is just a glorified Hello, World program. So, that's what you get! --- challenge-144/abigail/README.md | 21 +++++++++++++++++ challenge-144/abigail/awk/ch-1.awk | 13 +++++++++++ challenge-144/abigail/bash/ch-1.sh | 11 +++++++++ challenge-144/abigail/basic/ch-1.bas | 9 ++++++++ challenge-144/abigail/bc/ch-1.bc | 10 +++++++++ challenge-144/abigail/befunge-93/ch-1.bf93 | 4 ++++ challenge-144/abigail/c/ch-1.c | 16 +++++++++++++ challenge-144/abigail/cobol/ch-1.cb | 14 ++++++++++++ challenge-144/abigail/csh/ch-1.csh | 11 +++++++++ challenge-144/abigail/erlang/ch-1.erl | 15 +++++++++++++ challenge-144/abigail/forth/ch-1.fs | 5 +++++ challenge-144/abigail/fortran/ch-1.f90 | 14 ++++++++++++ challenge-144/abigail/go/ch-1.go | 15 +++++++++++++ challenge-144/abigail/java/ch-1.java | 13 +++++++++++ challenge-144/abigail/lua/ch-1.lua | 11 +++++++++ challenge-144/abigail/m4/ch-1.m4 | 1 + challenge-144/abigail/mmix/ch-1.mms | 18 +++++++++++++++ challenge-144/abigail/node/ch-1.js | 11 +++++++++ challenge-144/abigail/ocaml/ch-1.ml | 9 ++++++++ challenge-144/abigail/pascal/ch-1.p | 13 +++++++++++ challenge-144/abigail/perl/ch-1.pl | 36 ++++++++++++++++++++++++++++++ challenge-144/abigail/php/ch-1.php | 11 +++++++++ challenge-144/abigail/postscript/ch-1.ps | 10 +++++++++ challenge-144/abigail/python/ch-1.py | 11 +++++++++ challenge-144/abigail/r/ch-1.r | 9 ++++++++ challenge-144/abigail/rexx/ch-1.rexx | 9 ++++++++ challenge-144/abigail/ruby/ch-1.rb | 11 +++++++++ challenge-144/abigail/scheme/ch-1.scm | 9 ++++++++ challenge-144/abigail/sed/ch-1.sed | 12 ++++++++++ challenge-144/abigail/sql/ch-1.sql | 9 ++++++++ challenge-144/abigail/t/ctest.ini | 13 +++++++++++ challenge-144/abigail/t/input-1-1 | 1 + challenge-144/abigail/t/output-1-1.exp | 1 + challenge-144/abigail/tcl/ch-1.tcl | 9 ++++++++ 34 files changed, 385 insertions(+) create mode 100644 challenge-144/abigail/awk/ch-1.awk create mode 100644 challenge-144/abigail/bash/ch-1.sh create mode 100644 challenge-144/abigail/basic/ch-1.bas create mode 100644 challenge-144/abigail/bc/ch-1.bc create mode 100644 challenge-144/abigail/befunge-93/ch-1.bf93 create mode 100644 challenge-144/abigail/c/ch-1.c create mode 100644 challenge-144/abigail/cobol/ch-1.cb create mode 100644 challenge-144/abigail/csh/ch-1.csh create mode 100644 challenge-144/abigail/erlang/ch-1.erl create mode 100644 challenge-144/abigail/forth/ch-1.fs create mode 100644 challenge-144/abigail/fortran/ch-1.f90 create mode 100644 challenge-144/abigail/go/ch-1.go create mode 100644 challenge-144/abigail/java/ch-1.java create mode 100644 challenge-144/abigail/lua/ch-1.lua create mode 100644 challenge-144/abigail/m4/ch-1.m4 create mode 100644 challenge-144/abigail/mmix/ch-1.mms create mode 100644 challenge-144/abigail/node/ch-1.js create mode 100644 challenge-144/abigail/ocaml/ch-1.ml create mode 100644 challenge-144/abigail/pascal/ch-1.p create mode 100644 challenge-144/abigail/perl/ch-1.pl create mode 100644 challenge-144/abigail/php/ch-1.php create mode 100644 challenge-144/abigail/postscript/ch-1.ps create mode 100644 challenge-144/abigail/python/ch-1.py create mode 100644 challenge-144/abigail/r/ch-1.r create mode 100644 challenge-144/abigail/rexx/ch-1.rexx create mode 100644 challenge-144/abigail/ruby/ch-1.rb create mode 100644 challenge-144/abigail/scheme/ch-1.scm create mode 100644 challenge-144/abigail/sed/ch-1.sed create mode 100644 challenge-144/abigail/sql/ch-1.sql create mode 100644 challenge-144/abigail/t/ctest.ini create mode 100644 challenge-144/abigail/t/input-1-1 create mode 100644 challenge-144/abigail/t/output-1-1.exp create mode 100644 challenge-144/abigail/tcl/ch-1.tcl diff --git a/challenge-144/abigail/README.md b/challenge-144/abigail/README.md index 48a5093c46..554ccda4b8 100644 --- a/challenge-144/abigail/README.md +++ b/challenge-144/abigail/README.md @@ -2,14 +2,35 @@ ## Part 1 +* [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](lua/ch-1.erl) +* [Forth](lua/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) +* [MMIX](mmix/ch-1.mms) * [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) ## Part 2 diff --git a/challenge-144/abigail/awk/ch-1.awk b/challenge-144/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..4f87c14c56 --- /dev/null +++ b/challenge-144/abigail/awk/ch-1.awk @@ -0,0 +1,13 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk +# + +BEGIN { + print "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95" +} \ No newline at end of file diff --git a/challenge-144/abigail/bash/ch-1.sh b/challenge-144/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..3f5cc61adb --- /dev/null +++ b/challenge-144/abigail/bash/ch-1.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh +# + +echo "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95" \ No newline at end of file diff --git a/challenge-144/abigail/basic/ch-1.bas b/challenge-144/abigail/basic/ch-1.bas new file mode 100644 index 0000000000..ed7fc2883c --- /dev/null +++ b/challenge-144/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 "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95" \ No newline at end of file diff --git a/challenge-144/abigail/bc/ch-1.bc b/challenge-144/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..a73f52903b --- /dev/null +++ b/challenge-144/abigail/bc/ch-1.bc @@ -0,0 +1,10 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc +# +"4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95 +" +quit \ No newline at end of file diff --git a/challenge-144/abigail/befunge-93/ch-1.bf93 b/challenge-144/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..06001a7f07 --- /dev/null +++ b/challenge-144/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,4 @@ +< v "77, 82, 85, 86, 87, 91, 93, 94, 95" +55 + < v "38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, " + < v,_@#:< "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, " + > ^ \ No newline at end of file diff --git a/challenge-144/abigail/c/ch-1.c b/challenge-144/abigail/c/ch-1.c new file mode 100644 index 0000000000..1ed987f8dd --- /dev/null +++ b/challenge-144/abigail/c/ch-1.c @@ -0,0 +1,16 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o + */ + +int main (void) { + printf ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95\n"); + exit (0); +} \ No newline at end of file diff --git a/challenge-144/abigail/cobol/ch-1.cb b/challenge-144/abigail/cobol/ch-1.cb new file mode 100644 index 0000000000..5cd967efda --- /dev/null +++ b/challenge-144/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 "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95". + STOP RUN. diff --git a/challenge-144/abigail/csh/ch-1.csh b/challenge-144/abigail/csh/ch-1.csh new file mode 100644 index 0000000000..a1a34d3f27 --- /dev/null +++ b/challenge-144/abigail/csh/ch-1.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See ../README.md +# + +# +# Run as: csh ch-1.csh +# + +echo "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95" \ No newline at end of file diff --git a/challenge-144/abigail/erlang/ch-1.erl b/challenge-144/abigail/erlang/ch-1.erl new file mode 100644 index 0000000000..e7431f363b --- /dev/null +++ b/challenge-144/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 ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95\n"). \ No newline at end of file diff --git a/challenge-144/abigail/forth/ch-1.fs b/challenge-144/abigail/forth/ch-1.fs new file mode 100644 index 0000000000..6b2e57e241 --- /dev/null +++ b/challenge-144/abigail/forth/ch-1.fs @@ -0,0 +1,5 @@ +\ +\ See ../README.md +\ + +.( 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95) \ No newline at end of file diff --git a/challenge-144/abigail/fortran/ch-1.f90 b/challenge-144/abigail/fortran/ch-1.f90 new file mode 100644 index 0000000000..3a77b4cf66 --- /dev/null +++ b/challenge-144/abigail/fortran/ch-1.f90 @@ -0,0 +1,14 @@ +! +! See ../README.md +! + +! +! Run as: gfortran -o ch-1.o ch-1.f90; ./ch-1.o +! + +program ch1 + implicit none + write (*, '(A)', advance='no') "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, " + write (*, '(A)', advance='no') "34, 35, 38, 39, 46, 49, 51, 55, 57, 58, " + write (*, '(A)') "62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95" +end \ No newline at end of file diff --git a/challenge-144/abigail/go/ch-1.go b/challenge-144/abigail/go/ch-1.go new file mode 100644 index 0000000000..2328efdc3b --- /dev/null +++ b/challenge-144/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 ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95\n") +} \ No newline at end of file diff --git a/challenge-144/abigail/java/ch-1.java b/challenge-144/abigail/java/ch-1.java new file mode 100644 index 0000000000..34b246bfe5 --- /dev/null +++ b/challenge-144/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 ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95\n"); + } +} \ No newline at end of file diff --git a/challenge-144/abigail/lua/ch-1.lua b/challenge-144/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..310f260af9 --- /dev/null +++ b/challenge-144/abigail/lua/ch-1.lua @@ -0,0 +1,11 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua +-- + +print ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95") \ No newline at end of file diff --git a/challenge-144/abigail/m4/ch-1.m4 b/challenge-144/abigail/m4/ch-1.m4 new file mode 100644 index 0000000000..589eda7779 --- /dev/null +++ b/challenge-144/abigail/m4/ch-1.m4 @@ -0,0 +1 @@ +4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95 diff --git a/challenge-144/abigail/mmix/ch-1.mms b/challenge-144/abigail/mmix/ch-1.mms new file mode 100644 index 0000000000..e3e8095f46 --- /dev/null +++ b/challenge-144/abigail/mmix/ch-1.mms @@ -0,0 +1,18 @@ +% +% See ../README.md +% + +% +% Run as: mmixal -o ch-1.mmo ch-1.mms; mmix -q ch-1.mmo +% + LOC Data_Segment + GREG @ +Text BYTE "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, " + BYTE "39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, " + BYTE "85, 86, 87, 91, 93, 94, 95",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-144/abigail/node/ch-1.js b/challenge-144/abigail/node/ch-1.js new file mode 100644 index 0000000000..194b821605 --- /dev/null +++ b/challenge-144/abigail/node/ch-1.js @@ -0,0 +1,11 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js +// + +console . log ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95") \ No newline at end of file diff --git a/challenge-144/abigail/ocaml/ch-1.ml b/challenge-144/abigail/ocaml/ch-1.ml new file mode 100644 index 0000000000..e530d76fd3 --- /dev/null +++ b/challenge-144/abigail/ocaml/ch-1.ml @@ -0,0 +1,9 @@ +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: ocaml ch-1.ml *) +(* *) + +print_endline "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95"; \ No newline at end of file diff --git a/challenge-144/abigail/pascal/ch-1.p b/challenge-144/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..ce23f37768 --- /dev/null +++ b/challenge-144/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 ('4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95') +end. \ No newline at end of file diff --git a/challenge-144/abigail/perl/ch-1.pl b/challenge-144/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..e65ce40412 --- /dev/null +++ b/challenge-144/abigail/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/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 +# + +# +# And the upteenth time in succession, we'll use the divisors method. +# +# BBOOOOOOOOOOOOORRRRRRRRRRRRRRRIIIIIIIIIIIIIIINNNNNNNNNNNNGGGGGGGGGG +# +# Besides, ANY challenge of the form "print the first N terms of an +# OEIS sequence" is a trivial glorified hello world program. +# +# Semiprimes have either 3 (squares of primes) or 4 divisors (product +# of two primes), but should not be cubes of primes. They only cubes +# of primes < 100 are 8 and 27. +# + +use Math::Prime::Util qw [divisors]; +my %cubes = map {$_ => 1} 8, 27; + +say join ", " => grep {!$cubes {$_} && 3 <= divisors ($_) <= 4} 1 .. 100; diff --git a/challenge-144/abigail/php/ch-1.php b/challenge-144/abigail/php/ch-1.php new file mode 100644 index 0000000000..e7477300a4 --- /dev/null +++ b/challenge-144/abigail/php/ch-1.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/challenge-144/abigail/postscript/ch-1.ps b/challenge-144/abigail/postscript/ch-1.ps new file mode 100644 index 0000000000..62b38f7f97 --- /dev/null +++ b/challenge-144/abigail/postscript/ch-1.ps @@ -0,0 +1,10 @@ +%!PS +% +% See ../README.md +% + +% +% Run as: ps2ascii ch-1.ps +% + +(4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95) = \ No newline at end of file diff --git a/challenge-144/abigail/python/ch-1.py b/challenge-144/abigail/python/ch-1.py new file mode 100644 index 0000000000..3fdd6cfa92 --- /dev/null +++ b/challenge-144/abigail/python/ch-1.py @@ -0,0 +1,11 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py +# + +print ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95") \ No newline at end of file diff --git a/challenge-144/abigail/r/ch-1.r b/challenge-144/abigail/r/ch-1.r new file mode 100644 index 0000000000..4f115157d6 --- /dev/null +++ b/challenge-144/abigail/r/ch-1.r @@ -0,0 +1,9 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r +# + +cat ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95\n") \ No newline at end of file diff --git a/challenge-144/abigail/rexx/ch-1.rexx b/challenge-144/abigail/rexx/ch-1.rexx new file mode 100644 index 0000000000..2a1592bf01 --- /dev/null +++ b/challenge-144/abigail/rexx/ch-1.rexx @@ -0,0 +1,9 @@ +/* + * See ../README.md + */ + +/* + * Run as: rexx ch-1.rexx + */ + +say "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95" \ No newline at end of file diff --git a/challenge-144/abigail/ruby/ch-1.rb b/challenge-144/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..8e4d224ff8 --- /dev/null +++ b/challenge-144/abigail/ruby/ch-1.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb +# + +puts ("4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95"); \ No newline at end of file diff --git a/challenge-144/abigail/scheme/ch-1.scm b/challenge-144/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..7d9b4fa709 --- /dev/null +++ b/challenge-144/abigail/scheme/ch-1.scm @@ -0,0 +1,9 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + +(display "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95\n") \ No newline at end of file diff --git a/challenge-144/abigail/sed/ch-1.sed b/challenge-144/abigail/sed/ch-1.sed new file mode 100644 index 0000000000..547316ac9f --- /dev/null +++ b/challenge-144/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/.*/4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95/ \ No newline at end of file diff --git a/challenge-144/abigail/sql/ch-1.sql b/challenge-144/abigail/sql/ch-1.sql new file mode 100644 index 0000000000..c5df1097c5 --- /dev/null +++ b/challenge-144/abigail/sql/ch-1.sql @@ -0,0 +1,9 @@ +-- +-- See ../README.md +-- + +-- +-- Run as: sqlite3 < ch-1.sql +-- + +SELECT "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95"; \ No newline at end of file diff --git a/challenge-144/abigail/t/ctest.ini b/challenge-144/abigail/t/ctest.ini new file mode 100644 index 0000000000..88e8105056 --- /dev/null +++ b/challenge-144/abigail/t/ctest.ini @@ -0,0 +1,13 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Fixed output + +[1-1] +no_input = 1 + +[1-1/sed] +no_input = 0 diff --git a/challenge-144/abigail/t/input-1-1 b/challenge-144/abigail/t/input-1-1 new file mode 100644 index 0000000000..8d1c8b69c3 --- /dev/null +++ b/challenge-144/abigail/t/input-1-1 @@ -0,0 +1 @@ + diff --git a/challenge-144/abigail/t/output-1-1.exp b/challenge-144/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..589eda7779 --- /dev/null +++ b/challenge-144/abigail/t/output-1-1.exp @@ -0,0 +1 @@ +4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95 diff --git a/challenge-144/abigail/tcl/ch-1.tcl b/challenge-144/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..6d6cb07903 --- /dev/null +++ b/challenge-144/abigail/tcl/ch-1.tcl @@ -0,0 +1,9 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl +# + +puts "4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95" \ No newline at end of file -- cgit From 17f5562a76a8c54ea21fde11945f80ab697aed23 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 20 Dec 2021 19:06:27 +0100 Subject: Perl solution for week 144, part 2 --- challenge-144/abigail/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++++ challenge-144/abigail/t/ctest.ini | 1 + challenge-144/abigail/t/input-2-1 | 3 ++ challenge-144/abigail/t/output-2-1.exp | 3 ++ 4 files changed, 57 insertions(+) create mode 100644 challenge-144/abigail/perl/ch-2.pl create mode 100644 challenge-144/abigail/t/input-2-1 create mode 100644 challenge-144/abigail/t/output-2-1.exp diff --git a/challenge-144/abigail/perl/ch-2.pl b/challenge-144/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..c682d7a525 --- /dev/null +++ b/challenge-144/abigail/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +use List::Util qw [min]; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +sub ulam ($u1, $u2) { + die unless $u1 > 0 && $u2 > 0 && $u1 != $u2 + && $u1 == int $u1 && $u2 == int $u2; + ($u1, $u2) = ($u2, $u1) if $u1 > $u2; + + my @seen = ($u1, $u2); + my %sums; + $sums {$u1 + $u2} = 1; + my %todo = ($u1 + $u2, 1); + + while (%todo) { + my $next = min keys %todo; + delete $todo {$next}; + next if $sums {$next} > 1; + push @seen => $next; + return @seen if @seen >= 10; + + foreach my $seen (@seen) { + next if $seen == $next; + my $sum = $seen + $next; + $sums {$sum} ++; + $todo {$sum} = 1; + } + } +} + +$, = ", "; +say ulam /[0-9]+/g while <>; + diff --git a/challenge-144/abigail/t/ctest.ini b/challenge-144/abigail/t/ctest.ini index 88e8105056..d891360049 100644 --- a/challenge-144/abigail/t/ctest.ini +++ b/challenge-144/abigail/t/ctest.ini @@ -5,6 +5,7 @@ [names] 1-1 = Fixed output +2-1 = Given Examples [1-1] no_input = 1 diff --git a/challenge-144/abigail/t/input-2-1 b/challenge-144/abigail/t/input-2-1 new file mode 100644 index 0000000000..61becdbf4a --- /dev/null +++ b/challenge-144/abigail/t/input-2-1 @@ -0,0 +1,3 @@ +1 2 +2 3 +2 5 diff --git a/challenge-144/abigail/t/output-2-1.exp b/challenge-144/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..97b1a800ee --- /dev/null +++ b/challenge-144/abigail/t/output-2-1.exp @@ -0,0 +1,3 @@ +1, 2, 3, 4, 6, 8, 11, 13, 16, 18 +2, 3, 5, 7, 8, 9, 13, 14, 18, 19 +2, 5, 7, 9, 11, 12, 13, 15, 19, 23 -- cgit From ee4acfd9c139e158146c2faf096f4f94f994869b Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 20 Dec 2021 18:52:39 +0000 Subject: first pass --- challenge-144/james-smith/perl/ch-1.pl | 23 +++++++++++++++++++++++ challenge-144/james-smith/perl/ch-2.pl | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-144/james-smith/perl/ch-1.pl create mode 100644 challenge-144/james-smith/perl/ch-2.pl diff --git a/challenge-144/james-smith/perl/ch-1.pl b/challenge-144/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..97fe5b4e33 --- /dev/null +++ b/challenge-144/james-smith/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + + +my $N = 1000; +my @primes = (2); +my @semi_primes = (4); + +foreach my $p ( map { 1+2*$_ } 1..($N/4) ) { + map { ($p%$_)||(next) } @primes; + push @primes,$p; + push @semi_primes,grep {$_<=$N} map{$p*$_} @primes; +} + +say for sort {$a<=>$b} @semi_primes; + diff --git a/challenge-144/james-smith/perl/ch-2.pl b/challenge-144/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..024a36197b --- /dev/null +++ b/challenge-144/james-smith/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my (@seq,%seq_hash); + +say "@{[ ulam(1,2,100) ]}"; +say "@{[ ulam(2,3,100) ]}"; +say "@{[ ulam(2,5,100) ]}"; + +sub ulam { + my%seq_hash=map{$_,$_}my@seq=($_[0],my$n=$_[1]); + for(;scalar @seq<$_[2];++$n){ + push@seq,$seq_hash{$n}=$n if 1==grep{2*$_<$n&&$seq_hash{$n-$_}}@seq; + } + @seq; +} + -- cgit From fc60aacd55917860821d41f37ba4627a65a870b5 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Dec 2021 18:56:04 +0000 Subject: Update README.md --- challenge-144/james-smith/README.md | 132 ++++++------------------------------ 1 file changed, 22 insertions(+), 110 deletions(-) diff --git a/challenge-144/james-smith/README.md b/challenge-144/james-smith/README.md index 1dbf64a12e..caef49b926 100644 --- a/challenge-144/james-smith/README.md +++ b/challenge-144/james-smith/README.md @@ -1,5 +1,5 @@ -[< Previous 142](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-142/james-smith) | -[Next 144 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-144/james-smith) +[< Previous 143](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/james-smith) | +[Next 145 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-145/james-smith) # Perl Weekly Challenge #143 You can find more information about this weeks, and previous weeks challenges at: @@ -12,129 +12,41 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-144/james-smith -# Challenge 1 - Calculator +# Challenge 1 - Semiprimes -***You are given a string, `$s`, containing mathematical expression. Write a script to print the result of the mathematical expression. To keep it simple, please only accept `+ - * ()`.***` +***Write a script to generate all Semiprime number <= 100. (A semiprime is a number which is a multiple of two primes)***` ## The solution -The simple solution is just to "`eval`" the string, but where is the fun in that... We can either go for a tokenizing parser - where we create an array of elements brackets, symbols, numbers, or we can use regular expressions to reduce the equation. - -All students will remember BODMAS - Brackets, order, Division/Multiplication, Addition/Division. (or BIDMAS, PIDMAS, PEMDAS or whatever you used to remember it). So we have to break our prasing down into: - - * Brackets - * Order - we don't have this in our equations - * Division/Multiplication - we only have the latter - * Addition/Subtraction. - -So our logic becomes: - - * **B** - Find brackets without brackets within - for these we evaluate the contents using the same algorithm; - * **M** - If there are no brackets left - we then looking for multiplications from left to right and evaluate these. - * **AS** - If there are no brackets or multiplications we look at the addition/subtraction again left to right. - * This gives the following perl function: - ```perl -sub evaluate { - my $str = shift; - 1 while $str =~ s/\(\s*([^()]*?)\s*\)/ evaluate($1) /e; - 1 while $str =~ s/(-?\d+)\s*\*\s*(-?\d+)/ $1 * $2 /e; - 1 while $str =~ s/(-?\d+)\s*([-+])\s*(-?\d+)/$2 eq '+' ? $1+$3 : $1-$3/e; - return $str; +my $N = 1000; +my @primes = (2); +my @semi_primes = (4); + +foreach my $p ( map { 1+2*$_ } 1..($N/4) ) { + map { ($p%$_)||(next) } @primes; + push @primes,$p; + push @semi_primes,grep {$_<=$N} map{$p*$_} @primes; } -``` - -For small strings - this is about the same speed as `eval`, for larger strings not so, but in both cases it is "safer" as string `eval` of "tainted" input is a real security risk. - -## As a challenge infix->RPN converter than evaluate -On the Perl Programming Facebook Group - the use or RPN was mentioned - and so the challenge lead to reimplementing this by converting the infix notation to Reverse Polish and then evaluating the RPN stack. - -Infix `(a+b)*c+d` would become `a b + c * d +` in Reverse Polish Notation. - -To achieve this we set up a dispatch table - which stores methods for every operator we see in the string... -And then when we see an operator in the stream (either infix or rpn) we use the appropriate function. This simplifies the code considerably... - -```perl -my( @s, @o, %f ); ## @s <- stack, @o <- output, %f <- method "dispatch" table - -## THe 3 values are: -## precedence -## fn to apply when finding operator in infix stream -## fn to apply when finding operator in RPN stream - - ## Precedence -%f = ( ## Convertion function Infix -> RPN RPN processing function - '(' => [ 0, sub{ push @s, '(' }, ], - ')' => [ 0, sub{ push @o, $_ while ($_=pop@s) ne '(' }, ], - '*' => [ 2, sub{ push @o, pop @s while @s && $f{$s[-1]}[0]>1; push@s, '*' }, sub{ $s[-2] *= pop @s }], - '+' => [ 1, sub{ push @o, pop @s while @s && $f{$s[-1]}[0]; push@s, '+' }, sub{ $s[-2] += pop @s }], - '-' => [ 1, sub{ push @o, pop @s while @s && $f{$s[-1]}[0]; push@s, '-' }, sub{ $s[-2] -= pop @s }], -); - -## Two loops - first line converts infix to rpn -## second evaluates the rpn string. -sub evaluate_rpn { - @o= @s= (); ## Clear output and stack. - ## If operator use function in f hash to update output/stack - ## othewise it is a number and we push to output. - ($f{$_}) ? (&{$f{$_}[1]}) : (push@o,$_) for $_[0] =~ m{(-?\d+|[-+*()])}g; - ## If operator use function in f to update stack - ## otherwise we push the value onto the stack - ## ** we use reverse splice to reverse the string AND clear the stack at the - ## same time for the loop to work. - ($f{$_}) ? (&{$f{$_}[2]}) : (push@s,$_) for @o, reverse splice @s,0; - $s[0]; ## The result is the remaining value in the stack. -} +say for sort {$a<=>$b} @semi_primes; ``` -# Challenge 2 - Stealthy Number +# Challenge 2 - Ulam Sequence -***You are given a positive number, `$n`. Write a script to find out if the given number is Stealthy Number. A positive integer `N` is stealthy, if there exist positive integers `a`, `b`, `c`, `d` such that `a * b = c * d = N` and `a + b = c + d + 1`.*** +***You are given two positive numbers, `$u` and `$v`. Write a script to generate Ulam Sequence having at least 10 Ulam numbers where `$u` and `$v` are the first 2 Ulam numbers. +The standard Ulam sequence (the (1, 2)-Ulam sequence) starts with U1 = 1 and U2 = 2. Then for n > 2, Un is defined to be the smallest integer that is the sum of two distinct earlier terms in exactly one way and larger than all earlier terms.*** ## The solution -First we find all the factors of `N` (well the ones for where `N=a*b` and `a 1 } values %c) ? 1 : 0; -} -``` - -## An alternative solution - -Having played with the solution above - I realised (it should have been obvious) that the pairs of factors will be consecutive, e.g. 12 has 3 pairs 1,12 2,6, 3,4, and the pair that makes this a stealthy number is going to be `2+6 = 3+4+1`. - -We can therefore we don't need to keep an array of valid factors - only the previous factor. The code thus becomes: - -```perl -sub stealthy_number_1pass { - my($p,$n) = (1,@_); - $n%$_?1:$n/$p+$p-$n/$_-$_-1?($p=$_):(return 1)for 2..sqrt$n; - 0; -} -``` - -OK - that's a bit golfed. So lets unravel the ternaries in the middle to make the code more readable. - ```perl -sub stealthy_number_1pass { - my $n = shift; - my $p = 1; - foreach ( 2 .. sqrt $n ) { - next if $n % $_; - return 1 if $n/$p + $p == $n/$_ + $_ + 1; - $p = $_; +sub ulam { + my%seq_hash=map{$_,$_}my@seq=($_[0],my$n=$_[1]); + for(;scalar @seq<$_[2];++$n){ + push@seq,$seq_hash{$n}=$n if 1==grep{2*$_<$n&&$seq_hash{$n-$_}}@seq; } - return 0; + @seq; } ``` -- cgit From e8e56ec5fec50202a137e0b45d057811e8a5c510 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Dec 2021 18:56:39 +0000 Subject: Create blog.txt --- challenge-144/james-smith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-144/james-smith/blog.txt diff --git a/challenge-144/james-smith/blog.txt b/challenge-144/james-smith/blog.txt new file mode 100644 index 0000000000..7712f1c885 --- /dev/null +++ b/challenge-144/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-144 -- cgit From 59bc75f5714160e5e36543659eb44356c3494e47 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Dec 2021 18:56:51 +0000 Subject: Update README.md --- challenge-144/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-144/james-smith/README.md b/challenge-144/james-smith/README.md index caef49b926..09e1bfe578 100644 --- a/challenge-144/james-smith/README.md +++ b/challenge-144/james-smith/README.md @@ -1,6 +1,6 @@ [< Previous 143](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/james-smith) | [Next 145 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-145/james-smith) -# Perl Weekly Challenge #143 +# Perl Weekly Challenge #144 You can find more information about this weeks, and previous weeks challenges at: -- cgit From 11bec869bb9269ab76e427e90a5d6d367bfdc41c Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 21 Dec 2021 01:06:41 +0100 Subject: Make it even more boring --- challenge-144/abigail/perl/ch-1.pl | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/challenge-144/abigail/perl/ch-1.pl b/challenge-144/abigail/perl/ch-1.pl index e65ce40412..f2908f64f6 100644 --- a/challenge-144/abigail/perl/ch-1.pl +++ b/challenge-144/abigail/perl/ch-1.pl @@ -18,19 +18,16 @@ use experimental 'lexical_subs'; # # -# And the upteenth time in succession, we'll use the divisors method. +# And the upteenth time in succession, we'll use Math::Prime::Util # -# BBOOOOOOOOOOOOORRRRRRRRRRRRRRRIIIIIIIIIIIIIIINNNNNNNNNNNNGGGGGGGGGG +# BBBBBBBBBBOOOOOOOOOOORRRRRRRRRRRIIIIIIIIIIINNNNNNNNNNNGGGGGGGGGG # # Besides, ANY challenge of the form "print the first N terms of an # OEIS sequence" is a trivial glorified hello world program. # -# Semiprimes have either 3 (squares of primes) or 4 divisors (product -# of two primes), but should not be cubes of primes. They only cubes -# of primes < 100 are 8 and 27. +# Semiprimes have exactly 2 factors. # -use Math::Prime::Util qw [divisors]; -my %cubes = map {$_ => 1} 8, 27; +use Math::Prime::Util qw [factor]; -say join ", " => grep {!$cubes {$_} && 3 <= divisors ($_) <= 4} 1 .. 100; +say join ", " => grep {2 == factor $_} 1 .. 100; -- cgit From 55672727e94158136846b2d99c8d7576c11e1dac Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 21 Dec 2021 01:20:47 +0100 Subject: Simplified --- challenge-144/abigail/perl/ch-2.pl | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/challenge-144/abigail/perl/ch-2.pl b/challenge-144/abigail/perl/ch-2.pl index c682d7a525..d9dbb97da3 100644 --- a/challenge-144/abigail/perl/ch-2.pl +++ b/challenge-144/abigail/perl/ch-2.pl @@ -20,31 +20,24 @@ use List::Util qw [min]; # sub ulam ($u1, $u2) { - die unless $u1 > 0 && $u2 > 0 && $u1 != $u2 - && $u1 == int $u1 && $u2 == int $u2; - ($u1, $u2) = ($u2, $u1) if $u1 > $u2; - - my @seen = ($u1, $u2); - my %sums; - $sums {$u1 + $u2} = 1; - my %todo = ($u1 + $u2, 1); - - while (%todo) { - my $next = min keys %todo; - delete $todo {$next}; - next if $sums {$next} > 1; + my @seen = ($u1, $u2); + my %sums = ($u1 + $u2, 1); + + my $next; + while (%sums) { + $next = min keys %sums; # A heap saves a factor N/log N, but + # who cares about performance when we + # just want to do the first 10 numbers? + next if delete $sums {$next} > 1; push @seen => $next; return @seen if @seen >= 10; - foreach my $seen (@seen) { - next if $seen == $next; - my $sum = $seen + $next; - $sums {$sum} ++; - $todo {$sum} = 1; - } + $_ == $next || $sums {$_ + $next} ++ foreach @seen; } } + + $, = ", "; say ulam /[0-9]+/g while <>; -- cgit From 4a21974fd493b6f19d141c278badf706470682d1 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 21 Dec 2021 16:14:00 +0000 Subject: add some more methods and performance) --- challenge-144/james-smith/perl/ch-1.pl | 55 +++++++++++++++++++++++++++++----- challenge-144/james-smith/perl/ch-2.pl | 44 +++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/challenge-144/james-smith/perl/ch-1.pl b/challenge-144/james-smith/perl/ch-1.pl index 97fe5b4e33..39a78002de 100644 --- a/challenge-144/james-smith/perl/ch-1.pl +++ b/challenge-144/james-smith/perl/ch-1.pl @@ -8,16 +8,55 @@ use Test::More; use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); +my($C,$I) = (100,100_000); +#say foreach sp_loop(100);exit; +cmpthese( 100, { + 'sp' => sub { semiprimes($I); }, + 'sl' => sub { sp_loop( $I); }, + 'sm' => sub { sp_map( $I); }, +}); -my $N = 1000; -my @primes = (2); -my @semi_primes = (4); +sub sp_loop { + my $N = shift; + my @p; my %ph; + my @sp; + foreach my $t ( 2..$N ) { + my $prime = 1; + foreach(@p) { + next if $t%$_; + $prime = 0; + (push @sp,$t) && (last) if exists $ph{$t/$_}; + } + if( $prime ) { + push @p,$t; $ph{$t}=1; + } + } + @sp; +} + +sub sp_map { + my $N = shift; + my @primes = (2); + my @semi_primes = (4); -foreach my $p ( map { 1+2*$_ } 1..($N/4) ) { - map { ($p%$_)||(next) } @primes; - push @primes,$p; - push @semi_primes,grep {$_<=$N} map{$p*$_} @primes; + foreach my $p ( map { 1+2*$_ } 1..($N/4) ) { + map { ($p%$_)||(next) } @primes; + push @primes,$p; + push @semi_primes,grep {$_<=$N} map{$p*$_} @primes; + } + sort {$a<=>$b} @semi_primes; } -say for sort {$a<=>$b} @semi_primes; +sub semiprimes { + my $N = shift; + my @primes = (2); + my @semi_primes = (4); + + foreach my $p ( map { 1+2*$_ } 1..($N/4) ) { + map { ($p%$_)||(next) } @primes; + push @primes,$p; + ($p*$_>$N) ? (next) : (push @semi_primes,$p*$_) for @primes; + } + sort {$a<=>$b} @semi_primes; +} diff --git a/challenge-144/james-smith/perl/ch-2.pl b/challenge-144/james-smith/perl/ch-2.pl index 024a36197b..a3a2b2590e 100644 --- a/challenge-144/james-smith/perl/ch-2.pl +++ b/challenge-144/james-smith/perl/ch-2.pl @@ -8,13 +8,53 @@ use Test::More; use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); -my (@seq,%seq_hash); - +say "@{[ ulam_expanded(1,2,1000) ]}"; +say "@{[ ulam_map(1,2,1000) ]}"; +say "@{[ ulam(1,2,1000) ]}"; +say "@{[ ulam_expanded(1,2,100) ]}"; +say "@{[ ulam_map(1,2,100) ]}"; say "@{[ ulam(1,2,100) ]}"; say "@{[ ulam(2,3,100) ]}"; say "@{[ ulam(2,5,100) ]}"; +cmpthese( 200, { + 'u' => sub { ulam(1,2,1000) }, +# 'm' => sub { ulam_map(1,2,1000) }, + 'e' => sub { ulam_expanded(1,2,1000) }, +} ); + sub ulam { + my%h=map{$_,$_}my@s=($_[0],my$n=$_[1]); + for(my$c=0;@s<$_[2];++$n,$c=0){ + ($_>=$n/2)?(last):($h{$n-$_})&&$c++&&(last) for@s; + push@s,$h{$n}=$n if$c==1; + } + @s; +} + +sub ulam_expanded { + my ($m, $n, $l) = @_; + my @seq = ($m,$n); + my %seq_hash = ( $m => 1, $n => 1 ); + while( @seq < $l ) { + $n++; + my $count = 0; + foreach ( @seq ) { + last if $_ >= $n/2; + if( exists $seq_hash{ $n - $_ } ) { + $count++; + last if $count>1; + } + } + if( $count == 1 ) { + push @seq, $n; + $seq_hash{ $n } = 1; + } + } + return @seq; +} + +sub ulam_map { my%seq_hash=map{$_,$_}my@seq=($_[0],my$n=$_[1]); for(;scalar @seq<$_[2];++$n){ push@seq,$seq_hash{$n}=$n if 1==grep{2*$_<$n&&$seq_hash{$n-$_}}@seq; -- cgit From 5657ff55c37e028b0c8a2a6388d86e2c4bfedc85 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Tue, 21 Dec 2021 20:04:30 -0500 Subject: initial commit --- challenge-144/adam-russell/blog.txt | 0 challenge-144/adam-russell/blog1.txt | 0 challenge-144/adam-russell/cxx/ch-1.cxx | 0 challenge-144/adam-russell/cxx/ch-2.cxx | 0 challenge-144/adam-russell/perl/ch-1.pl | 47 ++++++++++++++++++++++++++++++++ challenge-144/adam-russell/perl/ch-2.pl | 16 +++++++++++ challenge-144/adam-russell/prolog/ch-1.p | 0 challenge-144/adam-russell/prolog/ch-2.p | 0 8 files changed, 63 insertions(+) create mode 100644 challenge-144/adam-russell/blog.txt create mode 100644 challenge-144/adam-russell/blog1.txt create mode 100644 challenge-144/adam-russell/cxx/ch-1.cxx create mode 100644 challenge-144/adam-russell/cxx/ch-2.cxx create mode 100644 challenge-144/adam-russell/perl/ch-1.pl create mode 100644 challenge-144/adam-russell/perl/ch-2.pl create mode 100644 challenge-144/adam-russell/prolog/ch-1.p create mode 100644 challenge-144/adam-russell/prolog/ch-2.p diff --git a/challenge-144/adam-russell/blog.txt b/challenge-144/adam-russell/blog.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-144/adam-russell/blog1.txt b/challenge-144/adam-russell/blog1.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-144/adam-russell/cxx/ch-1.cxx b/challenge-144/adam-russell/cxx/ch-1.cxx new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-144/adam-russell/cxx/ch-2.cxx b/challenge-144/adam-russell/cxx/ch-2.cxx new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-144/adam-russell/perl/ch-1.pl b/challenge-144/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..fa42aad875 --- /dev/null +++ b/challenge-144/adam-russell/perl/ch-1.pl @@ -0,0 +1,47 @@ +use strict; +use warnings; +## +# +## +use boolean; +use LWP::UserAgent; +use constant PRIME_URL => "http://primes.utm.edu/lists/small/100000.txt"; + +sub get_primes{ + my @primes; + my $ua = new LWP::UserAgent( + ssl_opts => {verify_hostname => 0} + ); + my $response = $ua->get(PRIME_URL); + my @lines = split(/\n/,$response->decoded_content); + foreach my $line (@lines){ + my @p = split(/\s+/, $line); + unless(@p < 10){ + push @primes, @p[1..(@p - 1)]; + } + } + return @primes; +} + +sub factor{ + my($n) = @_; + my @factors = (); + for my $j (2 .. sqrt($n)){ + push @factors, [$j, $n / $j] if $n % $j == 0; + } + return @factors; +} + +semiprime{ + my($n) = @_; + my @primes = get_primes; + my @factors = factor($n); + return false if @factors != 1; + my @prime_factors = grep {$factors[0]->[0] == $_ || $factors[0]->[1] == $_} @primes; + return false if @prime_factors != 2; + return true; +} + +MAIN:{ + semiprime_100; +} diff --git a/challenge-144/adam-russell/perl/ch-2.pl b/challenge-144/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..ad275c0103 --- /dev/null +++ b/challenge-144/adam-russell/perl/ch-2.pl @@ -0,0 +1,16 @@ +use strict; +use warnings; +## +# +## + +use constant ULAM_LIMIT => 10; + +sub ulam{ + my($u, $v) = @_; + +} + +MAIN:{ + ulam(1, 2); +} diff --git a/challenge-144/adam-russell/prolog/ch-1.p b/challenge-144/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-144/adam-russell/prolog/ch-2.p b/challenge-144/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..e69de29bb2 -- cgit From 0d9e18aca111f48c94ad0abf18a596f5c9c0ba71 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 24 Dec 2021 18:33:58 +0000 Subject: Add Brainfuck solution to challenge 001.2 --- challenge-001/paulo-custodio/Makefile | 8 +-- challenge-001/paulo-custodio/brainfuck.py | 2 +- challenge-001/paulo-custodio/brainfuck/ch-1.bf | 1 - challenge-001/paulo-custodio/brainfuck/ch-1.pl | 22 ------- challenge-001/paulo-custodio/brainfuck/ch-2.bf | 91 +++++++++++++++++++++++++- challenge-001/paulo-custodio/brainfuck/ch-2.pl | 36 ---------- 6 files changed, 92 insertions(+), 68 deletions(-) delete mode 100644 challenge-001/paulo-custodio/brainfuck/ch-1.bf delete mode 100644 challenge-001/paulo-custodio/brainfuck/ch-1.pl delete mode 100644 challenge-001/paulo-custodio/brainfuck/ch-2.pl diff --git a/challenge-001/paulo-custodio/Makefile b/challenge-001/paulo-custodio/Makefile index 24da326a16..c3c762d746 100644 --- a/challenge-001/paulo-custodio/Makefile +++ b/challenge-001/paulo-custodio/Makefile @@ -1,8 +1,2 @@ -all: brainfuck/ch-1.bf brainfuck/ch-2.bf +all: perl ../../challenge-001/paulo-custodio/test.pl - -brainfuck/ch-1.bf: brainfuck/ch-1.pl - perl brainfuck/ch-1.pl > $@ - -brainfuck/ch-2.bf: brainfuck/ch-2.pl - perl brainfuck/ch-2.pl > $@ diff --git a/challenge-001/paulo-custodio/brainfuck.py b/challenge-001/paulo-custodio/brainfuck.py index 7ef3443113..9d68e92acc 100644 --- a/challenge-001/paulo-custodio/brainfuck.py +++ b/challenge-001/paulo-custodio/brainfuck.py @@ -51,7 +51,7 @@ class TuringMachine: if do_trace: print("") elif op==",": - self.tape[self.ptr] = chr(sys.stdin.read(1)) + self.tape[self.ptr] = ord(sys.stdin.read(1)) elif op=="[": if self.tape[self.ptr]==0: self.find_close() diff --git a/challenge-001/paulo-custodio/brainfuck/ch-1.bf b/challenge-001/paulo-custodio/brainfuck/ch-1.bf deleted file mode 100644 index b1d94a8bb7..0000000000 --- a/challenge-001/paulo-custodio/brainfuck/ch-1.bf +++ /dev/null @@ -1 +0,0 @@ -[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++. diff --git a/challenge-001/paulo-custodio/brainfuck/ch-1.pl b/challenge-001/paulo-custodio/brainfuck/ch-1.pl deleted file mode 100644 index c9f6754286..0000000000 --- a/challenge-001/paulo-custodio/brainfuck/ch-1.pl +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env perl - -# Challenge 001 -# -# Challenge #1 -# Write a script to replace the character ‘e’ with ‘E’ in the string -# ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ -# is found in the string. - -# Output brainfuck program - -use Modern::Perl; - -my $s = "Perl Weekly Challenge"; -my $text = ($s =~ tr/e/E/)." ".$s; - -for (split //, $text) { - print "[-]"; # zero cell - print "+" x ord($_); # set cell to ASCII value of character - print "."; # output character -} -print "\n"; diff --git a/challenge-001/paulo-custodio/brainfuck/ch-2.bf b/challenge-001/paulo-custodio/brainfuck/ch-2.bf index 0a252de458..507fc83b2e 100644 --- a/challenge-001/paulo-custodio/brainfuck/ch-2.bf +++ b/challenge-001/paulo-custodio/brainfuck/ch-2.bf @@ -1 +1,90 @@ -[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++. +#!/usr/bin/bf + +[ +# Challenge 001 +# +# Challenge #2 +# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 +# through 20. However, any number divisible by 3 should be replaced by the word +# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both +# divisible by 3 and 5 become ‘fizzbuzz’. +] + +>++++[<+++++>-]< # Tape: *counter=20 +>+ # Tape: counter *index=1 +< # Tape: *counter index +[ # WHILE counter + # Tape: *counter index found_branch + + # MOVE index + >[->>+>+<<<] # Tape: counter *0 found index index + >>>[-<<<+>>>]< # Tape: counter index found *index + + # DIVIDE by 3 + >>>>+++<<<< # Tape: counter index found *index 0 0 0 3 + [->+>>+>-[<-]<[<<[->>>+<<<]>>>>+<<-<]<<] # divide + # Tape: 0 R 0 0 B' Q + >>>>[-]>[-]<<<< # cleanup all but R cursor on R + >>+<[-<->]<[[-]>>-<<]>>[<<+>>-]<< # check if R==0 + # Tape: counter index found 0 *divisible + [ # IF divisible by 3 + <<+ # Tape: counter index *inc_found + > + [-]>[-]< + >++++++++++[<++++++++++>-]<++. # "f" tape=102 + +++. # "i" tape=105 + >+++[<++++++>-]<-.. # "zz" tape=122 + [-] + + > + [-] + ] # END IF + + <<< # MOVE index + [->>+>+<<<] # Tape: counter *0 found index index + >>>[-<<<+>>>]< # Tape: counter index found *index + + # DIVIDE by 5 + >>>>+++++<<<< # Tape: counter index found *index 0 0 0 5 + [->+>>+>-[<-]<[<<[->>>+<<<]>>>>+<<-<]<<] # divide + # Tape: 0 R 0 0 B' Q + >>>>[-]>[-]<<<< # cleanup all but R cursor on R + >>+<[-<->]<[[-]>>-<<]>>[<<+>>-]<< # check if R==0 + # Tape: counter index found 0 *divisible + [ # IF divisible by 5 + <<+ # Tape: counter index *inc_found + > + + [-]>[-]< + >++++++++++[<++++++++++>-]<--. # "b" tape=98 + >++++[<+++++>-]<-. # "u" tape=117 + +++++.. # "zz" tape=122 + [-] + + > + [-] + ] # END IF + + << # Tape: counter index *found + >>+<[-<->]<[[-]>>-<<]>>[<<+>>-]<< # check if found==0 + [ # IF not yet found + + < # MOVE index + [->>>+>+<<<<] # Tape: counter *0 found 0 index index + >>>>[-<<<<+>>>>]< # Tape: counter index found 0 *index + + # PRINT NUMBER tape= 0 *M 0 0 0 + [>>>>++++++++++<<<<[->+>>+>-[<-]<[<<[->>>+<<<]>>>>+<<-<]<<] + ++++++++[->++++++<]>[-<+>]>>>>[-<<<<+>>>>]<[-]<<<]<[.<] + + >[-]>[-]>[-]>[-]<<<< # clear digits + < + [-] + ] # ENDIF + + [-]++++++++++.[-] # newline tape=0 + + << # Tape: *counter index found + + >+<- # Tape: *dec_counter inc_index +] # END WHILE counter diff --git a/challenge-001/paulo-custodio/brainfuck/ch-2.pl b/challenge-001/paulo-custodio/brainfuck/ch-2.pl deleted file mode 100644 index 65b62fd55a..0000000000 --- a/challenge-001/paulo-custodio/brainfuck/ch-2.pl +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env perl - -# Challenge 001 -# -# Challenge #2 -# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 -# through 20. However, any number divisible by 3 should be replaced by the word -# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both -# divisible by 3 and 5 become ‘fizzbuzz’. - -# Output brainfuck program - -use Modern::Perl; - -my $text = ""; -for my $n (1..20) { - if ($n%15==0) { - $text .= "fizzbuzz\n"; - } - elsif ($n%3==0) { - $text .= "fizz\n"; - } - elsif ($n%5==0) { - $text .= "buzz\n"; - } - else { - $text .= "$n\n"; - } -} - -for (split //, $text) { - print "[-]"; # zero cell - print "+" x ord($_); # set cell to ASCII value of character - print "."; # output character -} -print "\n"; -- cgit From c139fdc57151c6da87eca44ab018d5e3a2f25ea3 Mon Sep 17 00:00:00 2001 From: James Smith Date: Sat, 25 Dec 2021 02:10:46 +0000 Subject: Update README.md --- challenge-144/james-smith/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/challenge-144/james-smith/README.md b/challenge-144/james-smith/README.md index 09e1bfe578..d479af154d 100644 --- a/challenge-144/james-smith/README.md +++ b/challenge-144/james-smith/README.md @@ -20,6 +20,11 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-144/ja ## The solution +Rather than looping through each number to find if it is a semiprime - instead we find all the primes and multiply these together. +We realise we need the primes up to $N/2, and so compute them. Then for each prime we push all multiples of the prime with all +the previous primes (filtering for values less than or equal to $N) + +This method is faster than the loop method about 9x faster than the loop method for `$N = 10,000`. ```perl my $N = 1000; my @primes = (2); @@ -41,6 +46,8 @@ The standard Ulam sequence (the (1, 2)-Ulam sequence) starts with U1 = 1 and U2 ## The solution +For ulam numbers - we use an array and a hash to store previous ulam numbers. That allows us to quickly find those which have 1 unique partition. We do this with the grep in the 3rd line of the function. We then continue until the the array has the appropriate size. (We know there will be at least one solution the sum of the last two ulam numbers) + ```perl sub ulam { my%seq_hash=map{$_,$_}my@seq=($_[0],my$n=$_[1]); -- cgit From 767853a5f9ee388fc444981a4bd0d391c470a97d Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 25 Dec 2021 03:28:07 +0000 Subject: - Added solutions by James Smith. --- stats/pwc-current.json | 323 ++--- stats/pwc-language-breakdown-summary.json | 72 +- stats/pwc-language-breakdown.json | 2074 ++++++++++++++--------------- stats/pwc-leaders.json | 444 +++--- stats/pwc-summary-1-30.json | 40 +- stats/pwc-summary-121-150.json | 32 +- stats/pwc-summary-151-180.json | 42 +- stats/pwc-summary-181-210.json | 32 +- stats/pwc-summary-211-240.json | 100 +- stats/pwc-summary-241-270.json | 44 +- stats/pwc-summary-31-60.json | 96 +- stats/pwc-summary-61-90.json | 102 +- stats/pwc-summary-91-120.json | 106 +- stats/pwc-summary.json | 534 ++++---- 14 files changed, 2030 insertions(+), 2011 deletions(-) diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 2e0af509c6..b4b30b1404 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,10 +1,132 @@ { + "series" : [ + { + "data" : [ + { + "name" : "Athanasius", + "drilldown" : "Athanasius", + "y" : 4 + }, + { + "y" : 3, + "name" : "Dav