aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-06-12 20:37:27 -0300
committerandrezgz <andrezgz@gmail.com>2019-06-12 20:37:27 -0300
commitb2b3ec0f4213c9ca6f0f51c4d1f5e1df614b20c0 (patch)
treef8033b58a7c3898144dc315221f4f41cf38e0f5f
parent577a9eca20e108eaf205e24cb97641397683dd86 (diff)
downloadperlweeklychallenge-club-b2b3ec0f4213c9ca6f0f51c4d1f5e1df614b20c0.tar.gz
perlweeklychallenge-club-b2b3ec0f4213c9ca6f0f51c4d1f5e1df614b20c0.tar.bz2
perlweeklychallenge-club-b2b3ec0f4213c9ca6f0f51c4d1f5e1df614b20c0.zip
challenge-012 andrezgz solution
-rw-r--r--challenge-012/andrezgz/perl5/ch-1.pl28
-rw-r--r--challenge-012/andrezgz/perl5/ch-2.pl52
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-012/andrezgz/perl5/ch-1.pl b/challenge-012/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..4ede096485
--- /dev/null
+++ b/challenge-012/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-012/
+# Challenge #1
+# The numbers formed by adding one to the products of the smallest primes are called the Euclid Numbers
+# (see wiki). https://en.wikipedia.org/wiki/Euclid_number
+# Write a script that finds the smallest Euclid Number that is not prime.
+# This challenge was proposed by Laurent Rosenfeld.
+
+use strict;
+use warnings;
+
+my $n = 0;
+my $mult = 1;
+
+while(++$n){
+ if ( is_prime($n) ) {
+ $mult *= $n;
+ last unless(is_prime($mult + 1));
+ }
+}
+print $mult+1;
+
+sub is_prime {
+ my $n = shift;
+ my $upto = int($n / 2); #Divisors are <= n/2
+ return 1 == grep {$n % $_ == 0} (1 .. $upto);
+}
diff --git a/challenge-012/andrezgz/perl5/ch-2.pl b/challenge-012/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..e90d034b59
--- /dev/null
+++ b/challenge-012/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-012/
+# Challenge #2
+# Write a script that finds the common directory path, given a collection of paths and directory separator.
+# For example, if the following paths are supplied.
+# /a/b/c/d
+# /a/b/cd
+# /a/b/cc
+# /a/b/c/d/e
+# and the path separator is /. Your script should return /a/b as common directory path.
+
+
+use strict;
+use warnings;
+
+use constant SEPARATOR => '/';
+
+my $usage = "Usage: $0 <absolute-path-with-".SEPARATOR."-as-separator>\n";
+
+die $usage unless @ARGV > 0;
+foreach (@ARGV) {
+ die "'$_' is not an absolute path\n".$usage if(substr($_,0,1) ne SEPARATOR);
+}
+
+my @chosen = split SEPARATOR, shift @ARGV;
+shift @chosen; #first element is empty
+
+#Last common directory
+my $lcd = @chosen;
+
+#Compare each given absolute path with the first one
+PATH: foreach my $path (@ARGV) {
+ my @dirs = split SEPARATOR, $path;
+ shift @dirs; #first element is empty
+
+ my $matches = 0;
+ my $upto = ($lcd <= @dirs) ? $lcd : @dirs;
+ --$upto; #last directory index to compare
+
+ #Compare directories between this path and the chosen one
+ for my $index (0 .. $upto){
+ last if ($dirs[$index] ne $chosen[$index]);
+ $matches++;
+ }
+
+ $lcd = $matches if ($matches < $lcd);
+ last PATH if ($lcd == 0); #avoid checking others
+}
+
+splice(@chosen,$lcd);
+print 'Common directory path: /'.join(SEPARATOR, @chosen ); \ No newline at end of file