aboutsummaryrefslogtreecommitdiff
path: root/challenge-012
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2019-06-16 01:51:02 -0400
committerAdam Russell <ac.russell@live.com>2019-06-16 01:51:02 -0400
commit13e997d6a5281a34620e793b9f833301af200839 (patch)
tree5b30fd19af4433cfcbcbe258580139ec11a13e80 /challenge-012
parent478cd940f4ec3d503eb979ccf27fd3137118345f (diff)
downloadperlweeklychallenge-club-13e997d6a5281a34620e793b9f833301af200839.tar.gz
perlweeklychallenge-club-13e997d6a5281a34620e793b9f833301af200839.tar.bz2
perlweeklychallenge-club-13e997d6a5281a34620e793b9f833301af200839.zip
solutions for challenge 012
Diffstat (limited to 'challenge-012')
-rw-r--r--challenge-012/adam-russell/blog.txt1
-rw-r--r--challenge-012/adam-russell/perl5/ch-1.pl51
-rw-r--r--challenge-012/adam-russell/perl5/ch-2.pl46
3 files changed, 98 insertions, 0 deletions
diff --git a/challenge-012/adam-russell/blog.txt b/challenge-012/adam-russell/blog.txt
new file mode 100644
index 0000000000..87205d1a9a
--- /dev/null
+++ b/challenge-012/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/4250.html
diff --git a/challenge-012/adam-russell/perl5/ch-1.pl b/challenge-012/adam-russell/perl5/ch-1.pl
new file mode 100644
index 0000000000..9d0e8f3e4d
--- /dev/null
+++ b/challenge-012/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,51 @@
+use strict;
+use warnings;
+##
+# The numbers formed by adding one to the products of the smallest primes
+# are called the Euclid Numbers. Write a script that finds the smallest
+# Euclid Number that is not prime
+##
+use boolean;
+use LWP::UserAgent;
+use constant PRIME_URL => "http://primes.utm.edu/lists/small/10000.txt";
+
+sub get_primes{
+ my @primes;
+ my $ua = new LWP::UserAgent(
+ ssl_opts => {verify_hostname => 0}
+ );
+ my $response = $ua->get(PRIME_URL);
+ my @lines = split(/\n/,$response->decoded_content);
+ foreach my $line (@lines){
+ my @p = split(/\s+/, $line);
+ unless(@p < 10){
+ push @primes, @p[1..(@p - 1)];
+ }
+ }
+ return @primes;
+}
+
+sub find_nth_euclid{
+ return eval(join("*", @_)) + 1;
+}
+
+sub is_prime{
+ my($n) = @_;
+ for my $i (2 .. int(sqrt($n))){
+ return false if(($n % $i) == 0);
+ }
+ return true;
+}
+
+##
+# Main
+##
+my @primes = get_primes();
+foreach my $i (0..(@primes - 1)){
+ my $euclid = find_nth_euclid(@primes[0 .. $i]);
+ if(!is_prime($euclid)){
+ print "E_" . ($i + 1) . " = $euclid is composite.\n";
+ exit;
+ }
+}
+print "No composite Euclid Number found in the first " . @primes . " primes.\n";
diff --git a/challenge-012/adam-russell/perl5/ch-2.pl b/challenge-012/adam-russell/perl5/ch-2.pl
new file mode 100644
index 0000000000..af50610335
--- /dev/null
+++ b/challenge-012/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,46 @@
+use strict;
+use warnings;
+##
+# Write a script that finds the common directory path,
+# given a collection of paths and directory separator.
+##
+use boolean;
+
+##
+# Main
+##
+my @paths;
+my $common_path;
+my $seperator = <DATA>;
+chomp($seperator);
+while(<DATA>){
+ push @paths, [split(/\Q$seperator\E/, $_)];
+}
+for(my $i=0; $i < @paths; $i++){
+ my $in_common = true;
+ my $directory = shift @{$paths[$i]};
+ for(my $j=0; $j < @paths; $j++){
+ if($j != $i){
+ my $d = shift @{$paths[$j]};
+ if($directory && ($d ne $directory)){
+ $in_common = false;
+ last;
+ }
+ }
+ }
+ if($in_common){
+ $common_path .= $directory . $seperator;
+ }
+ else{
+ last;
+ }
+}
+chop($common_path);
+print "Path in common is $common_path.\n";
+
+__DATA__
+/
+/a/b/c/d
+/a/b/cd
+/a/b/cc
+/a/b/c/d/e