aboutsummaryrefslogtreecommitdiff
path: root/challenge-012
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-01-25 20:07:40 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-01-26 00:06:54 +0000
commit3fa58628535d4041c7cc648c005080ca88f18c18 (patch)
tree336fe3cc14f518f05e871ab974cc86a09a2fd8f6 /challenge-012
parent3d3900a2f0f69c54a34683e4e1b5da007b4af9d9 (diff)
downloadperlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.tar.gz
perlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.tar.bz2
perlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.zip
Replace tabs by spaces so that indentation looks correct
Diffstat (limited to 'challenge-012')
-rw-r--r--challenge-012/paulo-custodio/perl/ch-1.pl41
-rw-r--r--challenge-012/paulo-custodio/perl/ch-2.pl68
-rw-r--r--challenge-012/paulo-custodio/test.pl34
3 files changed, 71 insertions, 72 deletions
diff --git a/challenge-012/paulo-custodio/perl/ch-1.pl b/challenge-012/paulo-custodio/perl/ch-1.pl
index 532abb0f5c..e868d19cc0 100644
--- a/challenge-012/paulo-custodio/perl/ch-1.pl
+++ b/challenge-012/paulo-custodio/perl/ch-1.pl
@@ -5,7 +5,7 @@
# Challenge #1
# The numbers formed by adding one to the products of the smallest primes are
# called the Euclid Numbers (see wiki). Write a script that finds the smallest
-# Euclid Number that is not prime. This challenge was proposed by
+# Euclid Number that is not prime. This challenge was proposed by
# Laurent Rosenfeld.
use strict;
@@ -14,42 +14,41 @@ use 5.030;
# check if number is prime
sub is_prime {
- my($n) = @_;
+ my($n) = @_;
return 0 if $n <= 1;
return 1 if $n <= 3;
-
+
return 0 if ($n % 2)==0 || ($n % 3)==0;
-
+
for (my $i = 5; $i*$i <= $n; $i += 6) {
- return 0 if ($n % $i)==0 || ($n % ($i+2))==0;
- }
-
+ return 0 if ($n % $i)==0 || ($n % ($i+2))==0;
+ }
+
return 1;
}
# next prime
sub next_prime {
- my($n) = @_;
-
- return 2 if $n <= 1;
- my $p = $n;
- 1 while !is_prime(++$p);
- return $p;
+ my($n) = @_;
+
+ return 2 if $n <= 1;
+ my $p = $n;
+ 1 while !is_prime(++$p);
+ return $p;
}
sub euclid_iter {
- my $prime = 1;
- my $prime_prod = 1;
- return sub {
- $prime = next_prime($prime);
- $prime_prod *= $prime;
- return $prime_prod+1;
- };
+ my $prime = 1;
+ my $prime_prod = 1;
+ return sub {
+ $prime = next_prime($prime);
+ $prime_prod *= $prime;
+ return $prime_prod+1;
+ };
}
my $iter = euclid_iter();
my $p;
1 while (is_prime($p = $iter->()));
say $p;
-
diff --git a/challenge-012/paulo-custodio/perl/ch-2.pl b/challenge-012/paulo-custodio/perl/ch-2.pl
index 363f977ac9..d3d39d2184 100644
--- a/challenge-012/paulo-custodio/perl/ch-2.pl
+++ b/challenge-012/paulo-custodio/perl/ch-2.pl
@@ -10,7 +10,7 @@
# /a/b/cd
# /a/b/cc
# /a/b/c/d/e
-# and the path separator is /. Your script should return /a/b as common
+# and the path separator is /. Your script should return /a/b as common
# directory path.
use strict;
@@ -21,41 +21,41 @@ use Data::Dump 'dump';
# extract a common prefix, if one exists
sub extract_common_prefix {
- my($paths) = @_;
-
- # check if all paths have the same prefix
- my $dir;
- for my $i (0 .. $#{$paths}) {
- return unless @{$paths->[$i]}; # path empty
- if ($i == 0) {
- $dir = $paths->[$i][0]; # first path
- }
- else {
- return unless $dir eq $paths->[$i][0]; # not same prefix
- }
- }
-
- # all have $dir prefix, shift if out
- shift @$_ for (@$paths);
-
- return $dir;
-}
-
+ my($paths) = @_;
+
+ # check if all paths have the same prefix
+ my $dir;
+ for my $i (0 .. $#{$paths}) {
+ return unless @{$paths->[$i]}; # path empty
+ if ($i == 0) {
+ $dir = $paths->[$i][0]; # first path
+ }
+ else {
+ return unless $dir eq $paths->[$i][0]; # not same prefix
+ }
+ }
+
+ # all have $dir prefix, shift if out
+ shift @$_ for (@$paths);
+
+ return $dir;
+}
+
sub common_prefix {
- my($sep, @paths) = @_;
-
- # split paths by separator
- @paths = map {$_ = [split($sep, $_)]} @paths;
-
- # find common prefix
- my @prefix;
- while (defined(my $dir = extract_common_prefix(\@paths))) {
- push @prefix, $dir;
- }
-
- return join($sep, @prefix);
+ my($sep, @paths) = @_;
+
+ # split paths by separator
+ @paths = map {$_ = [split($sep, $_)]} @paths;
+
+ # find common prefix
+ my @prefix;
+ while (defined(my $dir = extract_common_prefix(\@paths))) {
+ push @prefix, $dir;
+ }
+
+ return join($sep, @prefix);
}
-
+
my($sep, @paths) = @ARGV;
say common_prefix($sep, @paths);
diff --git a/challenge-012/paulo-custodio/test.pl b/challenge-012/paulo-custodio/test.pl
index b1a5dbe3cb..db440a3a8f 100644
--- a/challenge-012/paulo-custodio/test.pl
+++ b/challenge-012/paulo-custodio/test.pl
@@ -8,30 +8,30 @@ use Test::More;
is capture("perl perl/ch-1.pl"), "30031\n";
is capture("perl perl/ch-2.pl / ".
- " /a/b/c/d ".
- " /a/b/cd ".
- " /a/b/cc ".
- " /a/b/c/d/e "), "/a/b\n";
+ " /a/b/c/d ".
+ " /a/b/cd ".
+ " /a/b/cc ".
+ " /a/b/c/d/e "), "/a/b\n";
is capture("perl perl/ch-2.pl : ".
- " :a:b:c:d ".
- " :a:b:cd ".
- " :a:b:cc ".
- " :a:b:c:d:e "), ":a:b\n";
+ " :a:b:c:d ".
+ " :a:b:cd ".
+ " :a:b:cc ".
+ " :a:b:c:d:e "), ":a:b\n";
is capture("perl perl/ch-2.pl / ".
- " /b/c/d ".
- " /a/b/c/d ".
- " /a/b/cd ".
- " /a/b/cc ".
- " /a/b/c/d/e "), "\n";
+ " /b/c/d ".
+ " /a/b/c/d ".
+ " /a/b/cd ".
+ " /a/b/cc ".
+ " /a/b/c/d/e "), "\n";
done_testing;
sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \r\t]*\n/\n/g;
- return $out;
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \r\t]*\n/\n/g;
+ return $out;
}