aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-008/gustavo-chaves/perl5/README.pod38
-rwxr-xr-xchallenge-008/gustavo-chaves/perl5/ch-1.pl14
-rwxr-xr-xchallenge-008/gustavo-chaves/perl5/ch-2.pl17
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;
+}