From 190edb56a911dc64a29b27643f0ca553f90b7e7e Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:57:22 +0100 Subject: Solution to task 1 --- challenge-241/jo-37/perl/ch-1.pl | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 challenge-241/jo-37/perl/ch-1.pl diff --git a/challenge-241/jo-37/perl/ch-1.pl b/challenge-241/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..93563fa2d3 --- /dev/null +++ b/challenge-241/jo-37/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; + +our ($tests, $examples, $diff); + +run_tests() if $tests || $examples; # does not return + +die < 2; +usage: $0 [-examples] [-tests] [-diff=D N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-diff=D + count arithmetic triplets having a difference of D + +N... + three or more integers + +EOS + + +### Input and Output + +say arith_triplets($diff, @ARGV); + + +### Implementation + +# Using graph theory to solve the task. We take the given integers as +# the vertices of a directed graph with their indices as unique +# identifiers and the integers as values. Two vertices are connected +# with an edge if and only if the indices are in correct order and the +# difference between the values equals the given difference. +# +# The adjacency matrix of a graph shows all edges, i.e. direct +# neighboring vertices. Furthermore, the squared adjacency matrix shows +# the number of 2-walks between any two vertices. In the constructed +# "forward only" graph every walk is actually a path, each 2-path +# corresponds to one arithmetic triplet for the given difference and the +# sum over the elements of this squared matrix is the requested number +# of arithmetic triplets. +# +# There is neither a need for the numbers to be in increasing order nor +# for the difference to be positive. + +sub arith_triplets { + my $diff = shift; + my $l = long @_; + # Build the adjacency matrix. + my $adj = + (sequence($l) > sequence($l)->dummy(0)) + & ($l - $l->dummy(0) == $diff); + + # Count the number of 2-paths in the graph. + ($adj x $adj)->sum; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + is arith_triplets(3 ,=> 0, 1, 4, 6, 7, 10), 2, 'example 1'; + is arith_triplets(2 ,=> 4, 5, 6, 7, 8, 9), 2, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is arith_triplets(1 ,=> 1, 2, 4, 5, 8, 9), 0, 'no triplet'; + is arith_triplets(2 ,=> 1, 3, 3, 5), 2, 'multiple paths'; + is arith_triplets(1 ,=> 9, 1, 8, 2, 7, 3), 1, 'not ordered'; + is arith_triplets(0 ,=> 1, 2, 1, 4, 1, 6), 1, 'zero diff'; + is arith_triplets(-1 ,=> 4, 3, 2, 1), 2, 'negative diff'; + } + + done_testing; + exit; +} -- cgit From 954143d8a4440b15eb3872d33309aac5d6b65321 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:57:35 +0100 Subject: Solution to task 2 --- challenge-241/jo-37/perl/ch-2.pl | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 challenge-241/jo-37/perl/ch-2.pl diff --git a/challenge-241/jo-37/perl/ch-2.pl b/challenge-241/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..a6b45b5be0 --- /dev/null +++ b/challenge-241/jo-37/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use Math::Prime::Util 'factor'; +use PDL; +use PDL::NiceSlice; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <qsortvec->((1))->unpdl; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is prime_order(11, 8, 27, 4), [11, 4, 8, 27], 'example 1'; + } + + SKIP: { + skip "tests" unless $tests; + + is prime_order(27, 8, 121, 49, 167, 131), + [131, 167, 49, 121, 8, 27], 'another example'; + } + + done_testing; + exit; +} -- cgit