aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-11-29 21:48:04 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-12-03 19:31:30 +0100
commit873d87e5709a4abe371aab03c195ea116449886b (patch)
tree38567cd3d336dee145befe8f822f5af2e8dbc495
parent34f2d91e2c7f9da9dfcd686494de8886d0dc9be5 (diff)
downloadperlweeklychallenge-club-873d87e5709a4abe371aab03c195ea116449886b.tar.gz
perlweeklychallenge-club-873d87e5709a4abe371aab03c195ea116449886b.tar.bz2
perlweeklychallenge-club-873d87e5709a4abe371aab03c195ea116449886b.zip
Solution to task 1
-rwxr-xr-xchallenge-141/jo-37/perl/ch-1.pl80
1 files changed, 80 insertions, 0 deletions
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 <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-count=C] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-count=C
+ Take C as the number of divisors. Default: 8
+
+N
+ Find the first N numbers having exactly C divisors.
+
+EOS
+
+
+### Input and Output
+
+main: {
+ my $gen_num_div = gen_num_div($count);
+ say $gen_num_div->() 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;
+}