From eac8ba024f5328cadea77c3088023a99f714bbfb Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 24 Jan 2022 13:54:51 +0100 Subject: Solution to task 1 --- challenge-149/jo-37/perl/ch-1.pl | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 challenge-149/jo-37/perl/ch-1.pl diff --git a/challenge-149/jo-37/perl/ch-1.pl b/challenge-149/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..ce0f8b0bcf --- /dev/null +++ b/challenge-149/jo-37/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Coro::Generator; +use Math::Prime::Util qw(lucasu todigits vecsum); + +our ($examples, $base); +$base ||= 10; + +run_tests() if $examples; # does not return + +die <() for 1 .. shift; +} + + +### Implementation + +sub gen_fib_digsum_seq { + # Considering only numbers with a digit sum less than 233. + state $fib = {map +(lucasu(1, -1, $_) => undef), 0 .. 12}; + + # Build a generator for numbers having a digit sum that is a + # Fibonacci number. + generator { + for (my $k = 0;; $k++) { + yield $k if exists $fib->{vecsum(todigits $k, $base)}; + } + } +} + + +### Examples and tests + +sub run_tests { + + my $fib_digsum_seq = gen_fib_digsum_seq(); + is [map $fib_digsum_seq->(), 1 .. 20], + [qw(0 1 2 3 5 8 10 11 12 14 17 20 21 23 26 30 32 35 41 44)], + 'example 1'; + + done_testing; + exit; +} -- cgit