diff options
Diffstat (limited to 'challenge-171/polettix/perl')
| -rw-r--r-- | challenge-171/polettix/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-171/polettix/perl/ch-2.pl | 13 |
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-171/polettix/perl/ch-1.pl b/challenge-171/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..11c197ea5d --- /dev/null +++ b/challenge-171/polettix/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; +use List::Util 'sum'; + +my $n = shift // 20; +my @abundants; +my $candidate = 945; +while (@abundants < $n) { + push @abundants, $candidate if is_abundant($candidate); + $candidate += 2; +} +say join ', ', @abundants; + +sub is_abundant ($n) { $n < sum(proper_positive_divisors($n)) } + +sub proper_positive_divisors ($n) { + return unless $n; + $n = -$n if $n < 0; + my (@lows, @highs) = (1); + my ($lo, $hi) = (2, $n); + while ($lo < $hi) { + if ($n % $lo == 0) { + push @lows, $lo; + $hi = int($n / $lo); + unshift @highs, $hi if $hi != $lo; + } + ++$lo; + } + return (@lows, @highs); +} diff --git a/challenge-171/polettix/perl/ch-2.pl b/challenge-171/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..ffb302d27f --- /dev/null +++ b/challenge-171/polettix/perl/ch-2.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +my $h1 = compose(sub { $_[0] + 1 }, sub { $_[0] * 2 }); +say $h1->(1); + +my $h2 = compose(sub { $_[0] * 2 }, sub { $_[0] + 1 }); +say $h2->(1); + +sub compose ($f, $g) { sub { $f->($g->(@_)) } } |
