From 6d2202d4486c244ffd29a92f953a840da927471f Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sun, 16 Jun 2019 02:05:07 -0400 Subject: Challenge 12 by Jaldhar H. Vyas --- challenge-012/jaldhar-h-vyas/perl5/ch-1.pl | 38 ++++++++++++++++++++++++++ challenge-012/jaldhar-h-vyas/perl5/ch-2.pl | 43 ++++++++++++++++++++++++++++++ challenge-012/jaldhar-h-vyas/perl6/ch-1.p6 | 36 +++++++++++++++++++++++++ challenge-012/jaldhar-h-vyas/perl6/ch-2.p6 | 22 +++++++++++++++ 4 files changed, 139 insertions(+) create mode 100755 challenge-012/jaldhar-h-vyas/perl5/ch-1.pl create mode 100755 challenge-012/jaldhar-h-vyas/perl5/ch-2.pl create mode 100755 challenge-012/jaldhar-h-vyas/perl6/ch-1.p6 create mode 100755 challenge-012/jaldhar-h-vyas/perl6/ch-2.p6 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 [ ...] + + [ ...] 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 -- cgit