diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-12 22:26:36 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-23 18:03:39 +0100 |
| commit | ff260192480fda25385374aedf44a2d672002acd (patch) | |
| tree | 06dceeca3462f07249aaab601c2b592913868433 | |
| parent | 6374ec0ba5c05d00a3a249fe8dec0d65719d23db (diff) | |
| download | perlweeklychallenge-club-ff260192480fda25385374aedf44a2d672002acd.tar.gz perlweeklychallenge-club-ff260192480fda25385374aedf44a2d672002acd.tar.bz2 perlweeklychallenge-club-ff260192480fda25385374aedf44a2d672002acd.zip | |
Challenge 008 task 1
| -rwxr-xr-x | challenge-008/jo-37/perl/ch-1.pl | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-008/jo-37/perl/ch-1.pl b/challenge-008/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..fad350e5b5 --- /dev/null +++ b/challenge-008/jo-37/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use bigint; +use List::Gen; +use Math::Prime::Util 'divisor_sum'; + +our ($tests, $examples, $verbose); + +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 perfect numbers + +EOS + + +### Input and Output + +gen_perfect()->take(shift)->say; + + +### Implementation + +# No need to consider odd perfect numbers as none is known yet. Thus +# restricting to those generated from Mersenne primes. +# Build a generator for even perfect primes. +sub gen_perfect { + <2..>->map(sub {2**($_ - 1) * (2**$_ - 1)}) + ->filter(sub {divisor_sum($_) == 2 * $_}); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is gen_perfect()->take(5), [6, 28, 496, 8128, 33550336], + 'task 1 from OEIS'; + } + + SKIP: { + skip "tests" unless $tests; + + is gen_perfect()->get(5), 8589869056, 'next from OEIS'; + } + + done_testing; + exit; +} |
