aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-16 08:38:51 +0100
committerGitHub <noreply@github.com>2019-06-16 08:38:51 +0100
commitfd3c332bde31049eb57310bdf5a5f91c338e5f63 (patch)
treeb96dee4aa581b56d2d015ac8ab59ac6a578abadb
parent9ba67f37ad4cf658161ecea9669568d0df4f1c40 (diff)
parent6d2202d4486c244ffd29a92f953a840da927471f (diff)
downloadperlweeklychallenge-club-fd3c332bde31049eb57310bdf5a5f91c338e5f63.tar.gz
perlweeklychallenge-club-fd3c332bde31049eb57310bdf5a5f91c338e5f63.tar.bz2
perlweeklychallenge-club-fd3c332bde31049eb57310bdf5a5f91c338e5f63.zip
Merge pull request #258 from jaldhar/challenge-012
Challenge 12 by Jaldhar H. Vyas
-rwxr-xr-xchallenge-012/jaldhar-h-vyas/perl5/ch-1.pl38
-rwxr-xr-xchallenge-012/jaldhar-h-vyas/perl5/ch-2.pl43
-rwxr-xr-xchallenge-012/jaldhar-h-vyas/perl6/ch-1.p636
-rwxr-xr-xchallenge-012/jaldhar-h-vyas/perl6/ch-2.p622
4 files changed, 139 insertions, 0 deletions
diff --git a/challenge-012/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-012/jaldhar-h-vyas/perl5/ch-1.pl
new file mode 100755
index 0000000000..23aceb4dca
--- /dev/null
+++ b/challenge-012/jaldhar-h-vyas/perl5/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+sub isPrime {
+ my ($n) = @_;
+
+ if ($n < 2) {
+ return undef;
+ }
+
+ if ($n == 2) {
+ return 1;
+ }
+
+ for my $i (2 .. sqrt($n)) {
+ if ($n % $i == 0) {
+ return undef;
+ }
+ }
+
+ return 1;
+}
+
+my $n = 1;
+my $primorial = 1;
+
+while (++$n) {
+ if (isPrime($n)) {
+ $primorial *= $n;
+ my $euclidNumber = $primorial + 1;
+ if (!isPrime($euclidNumber)) {
+ say $euclidNumber;
+ last;
+ }
+ }
+} \ No newline at end of file
diff --git a/challenge-012/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-012/jaldhar-h-vyas/perl5/ch-2.pl
new file mode 100755
index 0000000000..496d3ec841
--- /dev/null
+++ b/challenge-012/jaldhar-h-vyas/perl5/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+sub usage() {
+print <<"-USAGE-";
+Usage:
+ $0 [<args> ...]
+
+ [<args> ...] File paths.
+-USAGE-
+ exit 0;
+}
+
+# There must be atleast one argument and ach argument must start with a / to be
+# a valid file path.
+scalar @ARGV || usage();
+
+if (scalar grep { !/^\// } @ARGV) {
+ usage();
+}
+
+# Sort the arguments so the shortest is first because the common directory path
+# cannot be longer than the shortest path. Split each path into an array
+# of path segments and put those arrays in @path.
+my @paths = map { [ split q{/} ] } reverse sort @ARGV;
+
+# Compare the same segment of each path. If they are all the same, the
+# segment is part of the common directory path. If it isn't, we can stop.
+my @commonDirectoryPath;
+for my $segment (0 .. scalar @{$paths[0]} - 1) {
+ my $dir = @{$paths[0]}[$segment];
+ if (!scalar grep { !/$dir/ } map { @{$_}[$segment] } @paths) {
+ push @commonDirectoryPath, $dir;
+ } else {
+ last;
+ }
+}
+
+# Print the common directory path with the directory separator re-added. if
+# there were no matches in the previous step, the common directory path is /.
+say scalar @commonDirectoryPath > 1 ? join q{/}, @commonDirectoryPath : q{/}; \ No newline at end of file
diff --git a/challenge-012/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-012/jaldhar-h-vyas/perl6/ch-1.p6
new file mode 100755
index 0000000000..4b27154126
--- /dev/null
+++ b/challenge-012/jaldhar-h-vyas/perl6/ch-1.p6
@@ -0,0 +1,36 @@
+#!/usr/bin/perl6
+
+sub isPrime(Int $n) {
+
+ if ($n < 2) {
+ return False;
+ }
+
+
+ if $n == 2 {
+ return True;
+ }
+
+ for 2 .. $n.sqrt -> $i {
+ if $n % $i == 0 {
+ return False;
+ }
+ }
+
+ return True;
+}
+
+multi sub MAIN() {
+ my $primorial = 1;
+
+ for 1 .. * -> $n {
+ if isPrime($n) {
+ $primorial *= $n;
+ my $euclidNumber = $primorial + 1;
+ if !isPrime($euclidNumber) {
+ say $euclidNumber;
+ return;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/challenge-012/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-012/jaldhar-h-vyas/perl6/ch-2.p6
new file mode 100755
index 0000000000..a7c1a8fb2e
--- /dev/null
+++ b/challenge-012/jaldhar-h-vyas/perl6/ch-2.p6
@@ -0,0 +1,22 @@
+#!/usr/bin/perl6
+
+multi sub MAIN(
+ +@args where { $_.elems && $_.all ~~ /^\// } #= File paths.
+) {
+ my @paths = @args
+ .sort
+ .reverse
+ .map({ $_.split('/') });
+
+ my @commonDirectoryPath;
+ for 0 .. @paths[0].elems - 1 -> $segment {
+ my $dir = @paths.first()[$segment];
+ if @paths.map({ $_[$segment] }).all eq $dir {
+ push @commonDirectoryPath, $dir;
+ } else {
+ last;
+ }
+ }
+
+ say @commonDirectoryPath.elems > 1 ?? @commonDirectoryPath.join('/') !! '/';
+} \ No newline at end of file