From 873d87e5709a4abe371aab03c195ea116449886b Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 29 Nov 2021 21:48:04 +0100 Subject: Solution to task 1 --- challenge-141/jo-37/perl/ch-1.pl | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 challenge-141/jo-37/perl/ch-1.pl diff --git a/challenge-141/jo-37/perl/ch-1.pl b/challenge-141/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..6728041c18 --- /dev/null +++ b/challenge-141/jo-37/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util 'divisor_sum'; +use Coro::Generator; +use experimental 'signatures'; + +our ($tests, $examples, $count); +$count ||= 8; + +run_tests() if $tests || $examples; # does not return + +die <() for 1 .. shift; +} + + +### Implementation + +# Build a generator for numbers having exactly C divisors. Though this +# my be accomplished easily by just counting the divisors, the task +# itself seems to have very interesting aspects. At first glance the +# sequences for prime numbers C seem to be the primes to the power of +# C - 1. Sadly, I don't have time to investigate this in detail. + +sub gen_num_div ($c) { + generator { + for (my $n = 1;; $n++) { + yield $n if divisor_sum($n, 0) == $c; + } + } +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + is gen_num_div(8)->(), 24, 'example 1' + + } + + SKIP: { + skip "tests" unless $tests; + + my $gen_num_div_8 = gen_num_div(8); + $gen_num_div_8->() for 1 .. 50; + is $gen_num_div_8->(), 318, 'see http://oeis.org/A030626'; + + my $gen_num_div_6 = gen_num_div(6); + $gen_num_div_6->() for 1 .. 51; + is $gen_num_div_6->(), 412, 'see http://oeis.org/A030515'; + } + + done_testing; + exit; +} -- cgit