diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-03-12 17:46:11 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-03-12 17:46:11 +0100 |
| commit | e166b57d2a12ce6459a5975cef34a8ea8d040e38 (patch) | |
| tree | a9dd97eee2cfc897cb8984a8adc5fe962d5b6dc6 | |
| parent | c31583854c7b6df90b39c619efc47f91188c6a6f (diff) | |
| parent | 03052a2f6a81b5161f545a05bdb61861a7542b38 (diff) | |
| download | perlweeklychallenge-club-e166b57d2a12ce6459a5975cef34a8ea8d040e38.tar.gz perlweeklychallenge-club-e166b57d2a12ce6459a5975cef34a8ea8d040e38.tar.bz2 perlweeklychallenge-club-e166b57d2a12ce6459a5975cef34a8ea8d040e38.zip | |
Solutions to challenge 155
| -rwxr-xr-x | challenge-155/jo-37/perl/ch-1.pl | 82 | ||||
| -rwxr-xr-x | challenge-155/jo-37/perl/ch-2.pl | 82 |
2 files changed, 164 insertions, 0 deletions
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 <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [N] + +-examples + run the examples from the challenge + +-tests + run some tests + +N + Print the first N distinct fortunate numbers in ascending order. + +EOS + + +### Input and Output + +main: { + my $fn = gen_fortunate_numbers(); + my %fn; + # Collect the first N distinct fortunate numbers. + until (keys %fn == $ARGV[0]) { + $fn{$fn->()} = 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; +} 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 <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [N] + +-examples + run the examples from the challenge + +-tests + run some tests + +N + Print the N-th Pisano period + +EOS + + +### Input and Output + +say pisano(shift); + + +### Implementation + +# Build a generator for the Fibonacci sequence modulo N. +sub gen_fibonacci_mod ($n) { + my ($f1, $f2) = (0, 1); + + # Returns the last two numbers at the current position in the + # sequence. + generator { + while () { + yield [($f1, $f2) = ($f2, ($f1 + $f2) % $n)]; + } + } +} + +# Retrieve numbers of the Fibonacci sequence modulo N until the starting +# pair reappears. +sub pisano ($n) { + return 1 if $n == 1; + my ($fm, $m, $f) = (gen_fibonacci_mod($n), 0); + until (@$f ~~ @{[0, 1]}) { + $m++; + $f = $fm->(); + } + + $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; +} |
