diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-14 20:16:28 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-14 20:16:28 +0000 |
| commit | 5929b943e36af8fe476028b26f556dcaa3e44c5b (patch) | |
| tree | fb67f300e046e80e34edb03e28e75e4aec593774 | |
| parent | 7c00e0ff1de62736382c123421eb413847e9f5cd (diff) | |
| parent | 73a7fa000d858863ca87dca5aa6530dd971437e4 (diff) | |
| download | perlweeklychallenge-club-5929b943e36af8fe476028b26f556dcaa3e44c5b.tar.gz perlweeklychallenge-club-5929b943e36af8fe476028b26f556dcaa3e44c5b.tar.bz2 perlweeklychallenge-club-5929b943e36af8fe476028b26f556dcaa3e44c5b.zip | |
Merge pull request #3256 from pauloscustodio/008
Add Perl solution to challenge 008
| -rw-r--r-- | challenge-008/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/perl/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/test.pl | 32 |
4 files changed, 91 insertions, 0 deletions
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"), <<END; +6 +28 +496 +8128 +33550336 +END + +is capture("perl perl/ch-2.pl 'This' 'is' 'a test of the' 'center function'"), <<END; + This + is + a test of the +center function +END + +done_testing; + + +sub capture { + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \r\t]*\n/\n/g; + return $out; +} + |
