diff options
| author | lakpatashi <lakpatashi@gmail.com> | 2021-04-19 22:32:18 +0530 |
|---|---|---|
| committer | lakpatashi <lakpatashi@gmail.com> | 2021-04-19 22:32:18 +0530 |
| commit | 13a998813be4789f7b73797d523ac3c00ac65214 (patch) | |
| tree | 772b263a2df362b9da7244c19e0a2bcf6298b621 | |
| parent | 028d591894422f3513e799145cd86a1e27bcb771 (diff) | |
| download | perlweeklychallenge-club-13a998813be4789f7b73797d523ac3c00ac65214.tar.gz perlweeklychallenge-club-13a998813be4789f7b73797d523ac3c00ac65214.tar.bz2 perlweeklychallenge-club-13a998813be4789f7b73797d523ac3c00ac65214.zip | |
Finished challenge-012 with perl
| -rw-r--r-- | challenge-012/lakpatashi/README | 1 | ||||
| -rwxr-xr-x | challenge-012/lakpatashi/perl/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-012/lakpatashi/perl/ch-2.pl | 29 |
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-012/lakpatashi/README b/challenge-012/lakpatashi/README new file mode 100644 index 0000000000..bc153bd576 --- /dev/null +++ b/challenge-012/lakpatashi/README @@ -0,0 +1 @@ +Solution by lakpatashi diff --git a/challenge-012/lakpatashi/perl/ch-1.pl b/challenge-012/lakpatashi/perl/ch-1.pl new file mode 100755 index 0000000000..138f208415 --- /dev/null +++ b/challenge-012/lakpatashi/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use POSIX; +use strict; +use warnings; +use List::Util qw( min max sum); + + +my $m = 1000000; # max value for prime list +my @primes = buildPrimes($m); + +my $product = 1 ; +my $status = 0; +for my $x (@primes){ + $product *= $x; + #print "iter $x :: $product\n"; + #print "$product\n"; + unless(isPrime($product+1)){ + print "found :: ",$product+1,"\n"; + $status =1; + last; + } +} +print "Not found" unless($status); + +sub buildPrimes{ # building prime list + my $m=shift; + my @prime = (1)x($m+1); + @prime[0,1]=(0)x2; + for (my $p=2; $p*$p <= $m; $p++){ + if($prime[$p]==1){ + for(my $i= $p*$p; $i<= $m; $i+=$p){ + $prime[$i]=0; + } + } + } + @prime = grep{ $prime[$_] } 2..100; + return @prime; +} + +sub isPrime{ # check primality + my $n = shift; + my $nSq = sqrt $n; + for my $i (@primes){ + last if $i>$nSq; + return 0 unless $n % $i; + } + return 1; +} diff --git a/challenge-012/lakpatashi/perl/ch-2.pl b/challenge-012/lakpatashi/perl/ch-2.pl new file mode 100755 index 0000000000..80334e3e15 --- /dev/null +++ b/challenge-012/lakpatashi/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl +use POSIX; +use strict; +use warnings; +use List::Util qw( min max sum); + +my @arr = qw(/a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e); + +my $n = -1; +my @prevPath; +for my $x (@arr){ + my @path = split '/',$x; + if ($n == -1){ + $n = $#path; + }else{ + $n = min($n, $#path); + for my $i(reverse 1..$n){ + if($prevPath[$i] eq $path[$i]){ + $n = $i; + last; + } + $n = 0 if $i == 1; + } + print "no common" unless $n; + } + @prevPath = @path; +} +print "base dir:: ", +join '/', @prevPath[0..$n],"\n" if $n>0; |
