diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-05-18 03:38:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-18 03:38:23 +0100 |
| commit | a204dd67c6bab49c63668ca3c9cad944703f93a1 (patch) | |
| tree | 9a6dd91f7f28cdec1439c362354cd3ca6b072fce | |
| parent | 457bd04c36f49eadcd63cd6d201a96836b099f6d (diff) | |
| parent | 84211c1bfa87ce3a8b72a8643e99a3246c94b88c (diff) | |
| download | perlweeklychallenge-club-a204dd67c6bab49c63668ca3c9cad944703f93a1.tar.gz perlweeklychallenge-club-a204dd67c6bab49c63668ca3c9cad944703f93a1.tar.bz2 perlweeklychallenge-club-a204dd67c6bab49c63668ca3c9cad944703f93a1.zip | |
Merge pull request #156 from gnustavo/008
Gustavo Chaves's solutions to challenge 008
| -rw-r--r-- | challenge-008/gustavo-chaves/perl5/README.pod | 38 | ||||
| -rwxr-xr-x | challenge-008/gustavo-chaves/perl5/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-008/gustavo-chaves/perl5/ch-2.pl | 17 |
3 files changed, 69 insertions, 0 deletions
diff --git a/challenge-008/gustavo-chaves/perl5/README.pod b/challenge-008/gustavo-chaves/perl5/README.pod new file mode 100644 index 0000000000..0afb6c065b --- /dev/null +++ b/challenge-008/gustavo-chaves/perl5/README.pod @@ -0,0 +1,38 @@ +=pod + +=encoding utf8 + +=head1 #1 Perfect numbers + +=over 4 + +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 L<Wiki|https://en.wikipedia.org/wiki/Perfect_number> for +more information. This challenge was proposed by Laurent Rosenfeld. + +=back + +Mine is a straightforward implementation of the naive solution. But it's very +inefficient. On my computer it takes about two seconds to print the first four +perfect numbers (6, 28, 496, and 8128). However, I didn't have the patience to +wait for it to print the fifth number, as it's very large and the time +complexity of the algorithm that checks if a number is perfect is linear (O(n)) +on the number size. + +=head1 #2 Center lines + +=over 4 + +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. + +=back + +This is a simple problem with a simple solution which I implemented in a three +line long function. + +The script reads lines from STDIN or from the files passed to it as arguments +and invokes the C<center> function for all the lines, printing them centered. diff --git a/challenge-008/gustavo-chaves/perl5/ch-1.pl b/challenge-008/gustavo-chaves/perl5/ch-1.pl new file mode 100755 index 0000000000..18891aa6f3 --- /dev/null +++ b/challenge-008/gustavo-chaves/perl5/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl + +use 5.026; +use strict; +use autodie; +use warnings; +use List::Util qw(sum0); + +for (my ($n, $count) = (2, 0); $count < 5; ++$n) { + if ($n == sum0 grep {$n % $_ == 0} 1 .. $n/2) { + say $n; + ++$count; + } +} diff --git a/challenge-008/gustavo-chaves/perl5/ch-2.pl b/challenge-008/gustavo-chaves/perl5/ch-2.pl new file mode 100755 index 0000000000..4763c3b8d1 --- /dev/null +++ b/challenge-008/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl + +use 5.026; +use strict; +use autodie; +use warnings; +use List::Util qw(max); + +print center(<>); + +sub center { + my @lines = @_; + + my $max = max map {length} @lines; + + return map {' ' x (($max - length) / 2) . $_} @lines; +} |
