diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-12 18:04:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-12 18:04:34 +0100 |
| commit | 661915fa1328575d8183c75573f48e2c2f988053 (patch) | |
| tree | 340d23f46df48620915a49e51d93957161ba5067 | |
| parent | 51e797615ebe76694e05676ec3e44df8aaf27c89 (diff) | |
| parent | 66dd2463e4500e6d2fe28e1c5ccba2f508cc8ef7 (diff) | |
| download | perlweeklychallenge-club-661915fa1328575d8183c75573f48e2c2f988053.tar.gz perlweeklychallenge-club-661915fa1328575d8183c75573f48e2c2f988053.tar.bz2 perlweeklychallenge-club-661915fa1328575d8183c75573f48e2c2f988053.zip | |
Merge pull request #247 from aliciabielsa/branch-for-challenge-012
Week 12 challenges
| -rw-r--r-- | challenge-012/alicia-bielsa/perl5/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-012/alicia-bielsa/perl5/ch-2.pl | 35 |
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-012/alicia-bielsa/perl5/ch-1.pl b/challenge-012/alicia-bielsa/perl5/ch-1.pl new file mode 100644 index 0000000000..230307a6a5 --- /dev/null +++ b/challenge-012/alicia-bielsa/perl5/ch-1.pl @@ -0,0 +1,52 @@ +#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 List::Util qw(product);
+
+
+my $keepLooking = 1;
+my @aPrimeNumbers = ();
+my $currentPrimeNumber = 0;
+
+while ($keepLooking){
+
+ my $nextPrime = getNextPrime( $currentPrimeNumber );
+ push (@aPrimeNumbers, $nextPrime );
+ $currentPrimeNumber = $nextPrime;
+ my $euclidNumber = ( product @aPrimeNumbers ) +1;
+ if ( isPrimeNumber($euclidNumber) ){
+ print "Euclid number $euclidNumber is prime, keep looking\n";
+ } else {
+ print "Found the smallest Euclid Number that is not prime $euclidNumber\n";
+ $keepLooking = 0;
+ }
+}
+
+sub isPrimeNumber {
+
+ my $number = shift;
+ my $isPrime = 1;
+
+ foreach my $i ( 2..$number-1){
+ if ($number % $i == 0 ){
+ $isPrime = 0;
+ }
+ }
+ return $isPrime;
+}
+
+sub getNextPrime {
+
+ my $start = shift;
+ my $limit = 10000;
+ $start++;
+
+ foreach my $number ($start..$limit) {
+ if ( isPrimeNumber($number) ){
+ return $number;
+ }
+ }
+}
\ No newline at end of file diff --git a/challenge-012/alicia-bielsa/perl5/ch-2.pl b/challenge-012/alicia-bielsa/perl5/ch-2.pl new file mode 100644 index 0000000000..d0b45c7b14 --- /dev/null +++ b/challenge-012/alicia-bielsa/perl5/ch-2.pl @@ -0,0 +1,35 @@ +#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;
+
+my $separator = '/';
+my @aPaths =('/a/b/c/d/1/2','/a/b/c/d/x/x','/a/b/c/d/a/b/c','/a/b/c/d/e/f/g/h');
+my @aCommonPath = ();
+
+
+foreach my $path ( @aPaths ){
+
+ my @aDirectories = split ( $separator , $path );
+ unless ( scalar @aCommonPath ){
+ @aCommonPath = @aDirectories;
+ next;
+ }
+
+ foreach my $i ( 0..$#aDirectories ){
+ if ( defined($aCommonPath[$i]) && $aDirectories[$i] ne $aCommonPath[$i]){
+ splice (@aCommonPath, $i , $#aCommonPath - ($i - 1));
+ }
+ }
+}
+
+print "Common Directory path: " . join ( $separator,@aCommonPath ) ."\n";
+
+
\ No newline at end of file |
