aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-16 00:06:54 +0100
committerGitHub <noreply@github.com>2019-06-16 00:06:54 +0100
commit2e7f90d0ea013451580354af546c840ba1e50afb (patch)
tree5b17f502063c26456ad02e87209b6768edd8aa49
parent3183de3f76a3b2b140a1d2e58cc1bc447ab014ba (diff)
parentd33424fc7f4a83b97c60f8560d77ec928640904c (diff)
downloadperlweeklychallenge-club-2e7f90d0ea013451580354af546c840ba1e50afb.tar.gz
perlweeklychallenge-club-2e7f90d0ea013451580354af546c840ba1e50afb.tar.bz2
perlweeklychallenge-club-2e7f90d0ea013451580354af546c840ba1e50afb.zip
Merge pull request #254 from dmanto/branch-for-challenge-012
my proposed solutions for challenge-012, p5 1 & 2
-rw-r--r--challenge-012/daniel-mantovani/perl5/ch-1.pl42
-rw-r--r--challenge-012/daniel-mantovani/perl5/ch-2.pl47
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-012/daniel-mantovani/perl5/ch-1.pl b/challenge-012/daniel-mantovani/perl5/ch-1.pl
new file mode 100644
index 0000000000..9fdab60616
--- /dev/null
+++ b/challenge-012/daniel-mantovani/perl5/ch-1.pl
@@ -0,0 +1,42 @@
+# 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 Laurent Rosenfeld.
+
+use strict;
+use warnings;
+use v5.10;
+
+# we will need a function to test primality. As this is a
+# challenge and so far not very concerned about performance,
+# I will go for the "regex" primality test.
+# you can find more information at
+# https://iluxonchik.github.io/regular-expression-check-if-number-is-prime/
+# but let just say that to test if some n number is prime, we build a string
+# of n characters (with <char> x n), and let the regex engine try to divide
+# it in more than one equal parts of at least 2 chars. So if
+# we are not successful on that it means it is prime.
+# we use a digit as the char as we don't have to quote it. Any digit will
+# do, so we just pick "7"
+
+sub is_prime {
+ return ( 7 x shift ) !~ /^(77+)\1+$/;
+}
+
+# now we start generating primes from number 2, multiplying all of found
+# so far to get next primorial number, add 1 to get the corresponding
+# euclid number, check for primality, and loop until we get a non prime one
+
+my $prime = 2;
+my $euclid = 3;
+my @primes = ($prime);
+
+while ( is_prime $euclid) {
+ while ( !is_prime ++$prime ) { }; # find next prime
+ push @primes, $prime; # add to primes so far
+ $euclid = 1;
+ $euclid *= $_ for @primes; # this will be the primorial number
+ $euclid++; # and this is the new euclid number we found
+}
+
+say "First not prime Euclid is $euclid";
diff --git a/challenge-012/daniel-mantovani/perl5/ch-2.pl b/challenge-012/daniel-mantovani/perl5/ch-2.pl
new file mode 100644
index 0000000000..cedf9014ba
--- /dev/null
+++ b/challenge-012/daniel-mantovani/perl5/ch-2.pl
@@ -0,0 +1,47 @@
+# 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 v5.10;
+
+# our function will receive first the separator, then all the paths we want to check
+#
+sub find_common_path {
+ my ( $sep, @paths ) = @_;
+
+ # we start building a 2 dimmension array with all the parts on each path
+ # note the third argument of split function, that will consider
+ # a path ending with the separator diffent from the same path
+ # without a last separator
+ my @atoms;
+ for my $p (@paths) {
+ push @atoms, [ split $sep, $p, -1 ];
+ }
+
+ # now we just need to calculate a number (n), that will represent
+ # the amount of common parts on all paths
+ my $n = 0;
+ while ( $n < @{ $atoms[0] } ) {
+ my $same = 1;
+ my $atom = $atoms[0][$n];
+ for my $i ( 1 .. $#paths ) {
+ $same = 0 unless defined $atoms[$i][$n] && $atom eq $atoms[$i][$n];
+ }
+ last unless $same;
+ $n++;
+ }
+ $n--; # common part is just one less than the one that failed
+ return join $sep, @{ $atoms[0] }[ 0 .. $n ];
+}
+
+say "Common path of /a/b/c/d, /a/b/cd, /a/b/cc, /a/b/c/d/e is ",
+ find_common_path( '/', qw{/a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e} );