From 73a7fa000d858863ca87dca5aa6530dd971437e4 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 14 Jan 2021 09:21:50 +0000 Subject: Add Perl solution to challenge 008 --- challenge-008/paulo-custodio/README | 1 + challenge-008/paulo-custodio/perl/ch-1.pl | 34 +++++++++++++++++++++++++++++++ challenge-008/paulo-custodio/perl/ch-2.pl | 24 ++++++++++++++++++++++ challenge-008/paulo-custodio/test.pl | 32 +++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 challenge-008/paulo-custodio/README create mode 100644 challenge-008/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-008/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-008/paulo-custodio/test.pl diff --git a/challenge-008/paulo-custodio/README b/challenge-008/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-008/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-008/paulo-custodio/perl/ch-1.pl b/challenge-008/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..cde823ccf9 --- /dev/null +++ b/challenge-008/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# Challenge 008 +# +# Challenge #1 +# Write a script that computes the first five perfect numbers. A perfect number +# is an integer that is the sum of its positive proper divisors (all divisors +# except itself). Please check Wiki for more information. This challenge was +# proposed by Laurent Rosenfeld. + +use strict; +use warnings; +use 5.030; +use Math::Prime::Util 'next_prime', 'is_prime'; + +# Euclid proved that 2^(p−1)*(2^p − 1) is an even perfect number +# whenever 2p − 1 is prime +sub perfect_iter { + my $p = 1; + return sub { + while (1) { + $p = next_prime($p); # next prime number + my $f = (2**$p)-1; + return (2**($p-1))*$f if is_prime($f); + } + }; +} + + +my $n = shift || 5; +my $iter = perfect_iter(); +for (1..$n) { + say $iter->(); +} diff --git a/challenge-008/paulo-custodio/perl/ch-2.pl b/challenge-008/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..840e002f18 --- /dev/null +++ b/challenge-008/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +# Challenge 008 +# +# Challenge #2 +# Write a function, ‘center’, whose argument is a list of strings, which will +# be lines of text. The function should insert spaces at the beginning of the +# lines of text so that if they were printed, the text would be centered, and +# return the modified lines. + +use strict; +use warnings; +use 5.030; +use List::Util 'max'; + +sub center { + my(@lines) = @_; + my $max_len = max(map {length($_)} @lines); + @lines = map {$_ = " " x (($max_len-length($_))/2) . $_} @lines; + return @lines; +} + +my @lines = center(@ARGV); +say join "\n", @lines; diff --git a/challenge-008/paulo-custodio/test.pl b/challenge-008/paulo-custodio/test.pl new file mode 100644 index 0000000000..f4ebb29f9f --- /dev/null +++ b/challenge-008/paulo-custodio/test.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; +use Test::More; + +is capture("perl perl/ch-1.pl 5"), <