aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-11 11:26:37 +0100
committerGitHub <noreply@github.com>2019-06-11 11:26:37 +0100
commitcda9c3c918061f405548c1b4983ee46a01204695 (patch)
tree2eb28ec323c84383b6b7bba1e18b178d66623ae2
parent63f57cd0479718c61e8f7e999d026b49c9600655 (diff)
parente3f3db62bac5133fc78b912bb7a79a530af69842 (diff)
downloadperlweeklychallenge-club-cda9c3c918061f405548c1b4983ee46a01204695.tar.gz
perlweeklychallenge-club-cda9c3c918061f405548c1b4983ee46a01204695.tar.bz2
perlweeklychallenge-club-cda9c3c918061f405548c1b4983ee46a01204695.zip
Merge pull request #244 from shardiwal/shardiwal_012
Shardiwal 012
-rw-r--r--challenge-012/shardiwal/README1
-rw-r--r--challenge-012/shardiwal/perl5/ch-1.pl44
-rw-r--r--challenge-012/shardiwal/perl5/ch-2.pl58
3 files changed, 103 insertions, 0 deletions
diff --git a/challenge-012/shardiwal/README b/challenge-012/shardiwal/README
new file mode 100644
index 0000000000..c529c63c01
--- /dev/null
+++ b/challenge-012/shardiwal/README
@@ -0,0 +1 @@
+Solution by Rakesh Kumar Shardiwal
diff --git a/challenge-012/shardiwal/perl5/ch-1.pl b/challenge-012/shardiwal/perl5/ch-1.pl
new file mode 100644
index 0000000000..9ca638c94c
--- /dev/null
+++ b/challenge-012/shardiwal/perl5/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use feature qw(say);
+
+# 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.
+
+say scan_not_prime_euclid_number();
+
+sub scan_not_prime_euclid_number {
+ my $number = 1;
+ my $eucid_number;
+ while ( not $eucid_number ) {
+ $eucid_number = get_euclid_number($number);
+ $number++;
+ }
+ return $eucid_number;
+}
+
+sub get_euclid_number {
+ my $num = shift;
+ my $product = prime_product($num);
+ my $eucid_number = $product + 1;
+ return if is_prime($eucid_number);
+ # Euclid Number that is not prime.
+ return $eucid_number;
+}
+
+sub prime_product {
+ my $num = shift;
+ my $product = 1;
+ map { $product *= $_ if is_prime($_); } ( 1 .. $num );
+ return $product;
+}
+
+sub is_prime {
+ my $number = shift;
+ return 0 if grep { $number % $_ == 0 } ( 2 .. sqrt($number) );
+ return 1;
+}
diff --git a/challenge-012/shardiwal/perl5/ch-2.pl b/challenge-012/shardiwal/perl5/ch-2.pl
new file mode 100644
index 0000000000..7abbd7ee62
--- /dev/null
+++ b/challenge-012/shardiwal/perl5/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use feature qw(say);
+use Data::Dumper;
+
+# 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.
+
+my @path_list = qw(
+ /a/b/c/d
+ /a/b/cd
+ /a/b/cc
+ /a/b/c/d/e
+);
+
+say find_common_path( \@path_list, "/" );
+
+sub find_common_path {
+ my ( $path_list, $separator ) = @_;
+ my $path_mapping = common_path_mapping( $path_list, $separator );
+ my $common_path;
+ my $uses = 0;
+ while ( my ($path, $used) = each %$path_mapping ) {
+ my $used_no_of_times = scalar @$used;
+ if ( $used_no_of_times > $uses ){
+ $uses = $used_no_of_times;
+ $common_path = $path;
+ }
+ }
+ return $separator . $common_path;
+}
+
+sub common_path_mapping {
+ my ( $path_list, $separator ) = @_;
+ my %paths;
+ map {
+ my $line = $_;
+ my $dirs = _get_directories_in_path( $line, $separator );
+ my $i = 1;
+ foreach my $dir ( @$dirs ) {
+ next unless $dirs->[$i];
+ my $path_in_chunk = join( $separator, @$dirs[0..$i] );
+ push( @{ $paths{$path_in_chunk} }, $line);
+ $i++;
+ }
+ } @$path_list;
+ return \%paths;
+}
+
+sub _get_directories_in_path {
+ my ( $path, $separator ) = @_;
+ my @dirs = split /$separator/, $path;
+ shift @dirs;
+ return \@dirs;
+} \ No newline at end of file