diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-30 16:06:27 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-30 16:06:27 +0100 |
| commit | eef40563a50c1ebba58b404f2f9089c726e4aa0b (patch) | |
| tree | 4200116522827e6d6a6da9f7c447c48652bef092 | |
| parent | 4e47f9adafb6341b94ff0d1613258d40e6ff6ac6 (diff) | |
| download | perlweeklychallenge-club-eef40563a50c1ebba58b404f2f9089c726e4aa0b.tar.gz perlweeklychallenge-club-eef40563a50c1ebba58b404f2f9089c726e4aa0b.tar.bz2 perlweeklychallenge-club-eef40563a50c1ebba58b404f2f9089c726e4aa0b.zip | |
Add Perl solution
| -rw-r--r-- | challenge-171/paulo-custodio/perl/ch-1.pl | 60 | ||||
| -rw-r--r-- | challenge-171/paulo-custodio/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-171/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-171/paulo-custodio/t/test-2.yaml | 5 |
4 files changed, 100 insertions, 0 deletions
diff --git a/challenge-171/paulo-custodio/perl/ch-1.pl b/challenge-171/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..b67f270336 --- /dev/null +++ b/challenge-171/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +# Challenge 171 +# +# Task 1: Abundant Number +# Submitted by: Mohammad S Anwar +# +# Write a script to generate first 20 Abundant Odd Numbers. +# +# According to wikipedia, +# +# A number n for which the sum of divisors σ(n) > 2n, or, equivalently, the +# sum of proper divisors (or aliquot sum) s(n) > n. +# +# +# For example, 945 is the first Abundant Odd Number. +# +# Sum of divisors: +# 1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 135 + 189 + 315 = 975 + +use Modern::Perl; +use List::Util 'sum'; + +sub divisors { + my($n) = @_; + my(@div_low, @div_high); + for (my $i = 1; $i <= sqrt($n); $i++) { + if ($n%$i == 0) { + push @div_low, $i; + unshift @div_high, $n/$i if $n/$i != $i; + } + } + return (@div_low, @div_high); +} + +sub proper_divisors { + my($n) = @_; + my @div = divisors($n); + return @div[0..$#div-1]; +} + +sub is_abundant { + my($n) = @_; + return sum(proper_divisors($n)) > $n; +} + +sub abundant_numbers { + my($N) = @_; + my @abundant; + my $n = 1; + while (@abundant < $N) { + push @abundant, $n if is_abundant($n); + $n++; + } + return @abundant; +} + +@ARGV==1 or die "usage: ch-1.pl n\n"; +my $N = shift; +say join ", ", abundant_numbers($N); diff --git a/challenge-171/paulo-custodio/perl/ch-2.pl b/challenge-171/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..45c3da0abf --- /dev/null +++ b/challenge-171/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +# Challenge 171 +# +# Task 2: First-class Function +# Submitted by: Mohammad S Anwar +# +# Create sub compose($f, $g) which takes in two parameters $f and $g as +# subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x) = +# $f->($g->($x)) +# +# e.g. +# +# $f = (one or more parameters function) +# $g = (one or more parameters function) +# +# $h = compose($f, $g) +# $f->($g->($x,$y, ..)) == $h->($x, $y, ..) for any $x, $y, ... + +use Modern::Perl; + +sub compose { + my($f, $g) = @_; + return sub { return $f->($g->(@_)); }; +} + +sub times3 { return 3*$_[0]; } +sub times5 { return 5*$_[0]; } +my $h = compose(\×3, \×5); +say $h->(shift); diff --git a/challenge-171/paulo-custodio/t/test-1.yaml b/challenge-171/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5b9eac3ad8 --- /dev/null +++ b/challenge-171/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 20 + input: + output: 12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90 diff --git a/challenge-171/paulo-custodio/t/test-2.yaml b/challenge-171/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..685edf6542 --- /dev/null +++ b/challenge-171/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 2 + input: + output: 30 |
