From 1969434787f163f6da03c9c3cb6ea2c31cf97f9e Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 7 Mar 2022 16:19:14 +0100 Subject: Solution to task 1 --- challenge-155/jo-37/perl/ch-1.pl | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 challenge-155/jo-37/perl/ch-1.pl diff --git a/challenge-155/jo-37/perl/ch-1.pl b/challenge-155/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..ad2c142392 --- /dev/null +++ b/challenge-155/jo-37/perl/ch-1.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use bigint; +use Math::Prime::Util qw(next_prime is_prime); +use Coro::Generator; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <()} = undef; + } + # Present in ascending order. + say for sort {$a <=> $b} keys %fn; +} + + +### Implementation + +# Build a generator for fortunate numbers. The resulting sequence is +# neither sorted nor does it consist of distinct values only. +sub gen_fortunate_numbers { + my $pn = 1; + my $p = 1; + + generator { + while () { + $pn *= ($p = next_prime($p)); + for (my $m = 2;; $m++) { + yield($m), last if is_prime($pn + $m); + } + } + } +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + my $fn = gen_fortunate_numbers(); + is [map $fn->(), 1 .. 9], [3, 5, 7, 13, 23, 17, 19, 23, 37], + 'the raw sequence: neither sorted nor distinct' + } + + SKIP: { + skip "tests" unless $tests; + + my $fn = gen_fortunate_numbers(); + $fn->() for 1 .. 57; + is $fn->(), 331, '#58 according to OEIS'; + } + + done_testing; + exit; +} -- cgit From 03052a2f6a81b5161f545a05bdb61861a7542b38 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 7 Mar 2022 18:10:22 +0100 Subject: Solution to task 2 --- challenge-155/jo-37/perl/ch-2.pl | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 challenge-155/jo-37/perl/ch-2.pl diff --git a/challenge-155/jo-37/perl/ch-2.pl b/challenge-155/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..cf89d00585 --- /dev/null +++ b/challenge-155/jo-37/perl/ch-2.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Coro::Generator; +use experimental qw(signatures smartmatch); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <(); + } + + $m; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is pisano(3), 8, 'task 2'; + } + + SKIP: { + skip "tests" unless $tests; + + my $fm = gen_fibonacci_mod(3); + is [map $fm->()[0], 1 .. 10], [1, 1, 2, 0, 2, 2, 1, 0, 1, 1], + 'Fibonacci mod 3'; + + is pisano(50), 300, 'from OEIS'; + } + + done_testing; + exit; +} -- cgit