aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-30 09:15:05 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-30 09:15:05 +0100
commit28467bf992233263b87198941bc0eb9a0ddf9e0c (patch)
treed72b104586ad59f07f732b553ac6eeaa691cae0f
parentf7e17efb23f4b03f6baef73bfb22521c06d6e47f (diff)
downloadperlweeklychallenge-club-28467bf992233263b87198941bc0eb9a0ddf9e0c.tar.gz
perlweeklychallenge-club-28467bf992233263b87198941bc0eb9a0ddf9e0c.tar.bz2
perlweeklychallenge-club-28467bf992233263b87198941bc0eb9a0ddf9e0c.zip
Add Perl solution
-rw-r--r--challenge-167/paulo-custodio/perl/ch-1.pl3
-rw-r--r--challenge-167/paulo-custodio/perl/ch-2.pl18
-rw-r--r--challenge-168/paulo-custodio/perl/ch-1.pl56
-rw-r--r--challenge-168/paulo-custodio/perl/ch-2.pl65
-rw-r--r--challenge-168/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-168/paulo-custodio/t/test-2.yaml25
6 files changed, 161 insertions, 11 deletions
diff --git a/challenge-167/paulo-custodio/perl/ch-1.pl b/challenge-167/paulo-custodio/perl/ch-1.pl
index 92dff3782f..6ee40c97f8 100644
--- a/challenge-167/paulo-custodio/perl/ch-1.pl
+++ b/challenge-167/paulo-custodio/perl/ch-1.pl
@@ -64,11 +64,10 @@ sub next_circular_prime {
return $n;
}
-@ARGV or die "usage: ch-1.pl n\n";
+@ARGV==1 or die "usage: ch-1.pl n\n";
my $n=shift;
my @cprimes;
my $cp=1;
-my %seen;
while (@cprimes<$n) {
$cp=next_circular_prime($cp);
push @cprimes,$cp if $cp>=100;
diff --git a/challenge-167/paulo-custodio/perl/ch-2.pl b/challenge-167/paulo-custodio/perl/ch-2.pl
index 180fbfdc48..65e303772e 100644
--- a/challenge-167/paulo-custodio/perl/ch-2.pl
+++ b/challenge-167/paulo-custodio/perl/ch-2.pl
@@ -26,15 +26,15 @@ use List::Util 'sum';
use constant g => 7;
my @p = (
- 0.99999999999980993,
- 676.5203681218851,
- -1259.1392167224028,
- 771.32342877765313,
- -176.61502916214059,
- 12.507343278686905,
- -0.13857109526572012,
- 9.9843695780195716e-6,
- 1.5056327351493116e-7
+ 0.99999999999980993227684700473478,
+ 676.520368121885098567009190444019,
+ -1259.13921672240287047156078755283,
+ 771.3234287776530788486528258894,
+ -176.61502916214059906584551354,
+ 12.507343278686904814458936853,
+ -0.13857109526572011689554707,
+ 9.984369578019570859563e-6,
+ 1.50563273514931155834e-7
);
use constant EPSILON => 1e-07;
diff --git a/challenge-168/paulo-custodio/perl/ch-1.pl b/challenge-168/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..eda2430282
--- /dev/null
+++ b/challenge-168/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+# Challenge 168
+#
+# Task 1: Perrin Prime
+# Submitted by: Roger Bell_West
+#
+# The Perrin sequence is defined to start with [3, 0, 2]; after that, term N is
+# the sum of terms N-2 and N-3. (So it continues 3, 2, 5, 5, 7, ….)
+#
+# A Perrin prime is a number in the Perrin sequence which is also a prime
+# number.
+#
+# Calculate the first 13 Perrin Primes.
+#
+# f(13) = [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193,
+# 792606555396977]
+
+use Modern::Perl;
+
+sub is_prime {
+ my($n) = @_;
+ return 0 if $n <= 1;
+ return 1 if $n <= 3;
+ return 0 if ($n % 2)==0 || ($n % 3)==0;
+ for (my $i = 5; $i*$i <= $n; $i += 6) {
+ return 0 if ($n % $i)==0 || ($n % ($i+2))==0;
+ }
+ return 1;
+}
+
+sub perrin_seq {
+ my(@t)=(3,0,2);
+ return sub {
+ push @t, $t[-2]+$t[-3];
+ return shift @t;
+ }
+}
+
+sub perrin_prime_seq {
+ my $it=perrin_seq();
+ my %seen;
+ return sub {
+ while (1) {
+ my $n=$it->();
+ return $n if is_prime($n) && !$seen{$n}++;
+ }
+ };
+}
+
+@ARGV==1 or die "usage: ch-1.pl n\n";
+my $n=shift;
+my @pp;
+my $it=perrin_prime_seq();
+push @pp, $it->() while @pp<$n;
+say join ", ", sort {$a<=>$b} @pp;
diff --git a/challenge-168/paulo-custodio/perl/ch-2.pl b/challenge-168/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..b3ce77932c
--- /dev/null
+++ b/challenge-168/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+
+# Challenge 168
+#
+# Task 2: Home Prime
+# Submitted by: Mohammad S Anwar
+#
+# You are given an integer greater than 1.
+#
+# Write a script to find the home prime of the given number.
+#
+# In number theory, the home prime HP(n) of an integer n greater than 1 is the
+# prime number obtained by repeatedly factoring the increasing concatenation of
+# prime factors including repetitions.
+#
+# Further information can be found on Wikipedia and OEIS.
+# Example
+#
+# As given in the Wikipedia page,
+#
+# HP(10) = 773, as 10 factors as 2×5 yielding HP10(1) = 25, 25 factors as 5×5
+# yielding HP10(2) = HP25(1) = 55, 55 = 5×11 implies HP10(3) = HP25(2) = HP55(1)
+# = 511, and 511 = 7×73 gives HP10(4) = HP25(3) = HP55(2) = HP511(1) = 773, a
+# prime number.
+
+use Modern::Perl;
+
+sub is_prime {
+ my($n) = @_;
+ return 0 if $n <= 1;
+ return 1 if $n <= 3;
+ return 0 if ($n % 2)==0 || ($n % 3)==0;
+ for (my $i = 5; $i*$i <= $n; $i += 6) {
+ return 0 if ($n % $i)==0 || ($n % ($i+2))==0;
+ }
+ return 1;
+}
+
+sub prime_factors {
+ my($n) = @_;
+ my @factors;
+ my $p = 0;
+ while ($n > 1) {
+ do { $p++; } while (!is_prime($p));
+ while ($n % $p == 0) {
+ push @factors, $p;
+ $n /= $p;
+ }
+ }
+ return @factors;
+}
+
+sub home_prime {
+ my($n) = @_;
+ die unless $n > 1;
+ while (!is_prime($n)) {
+ my @factors = prime_factors($n);
+ $n = 0+join '', @factors;
+ }
+ return $n;
+}
+
+
+@ARGV==1 or die "usage: ch-1.pl n\n";
+say home_prime(shift);
diff --git a/challenge-168/paulo-custodio/t/test-1.yaml b/challenge-168/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..03e75b6351
--- /dev/null
+++ b/challenge-168/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 13
+ input:
+ output: 2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977
diff --git a/challenge-168/paulo-custodio/t/test-2.yaml b/challenge-168/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..65e1ee34d9
--- /dev/null
+++ b/challenge-168/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 211
+- setup:
+ cleanup:
+ args: 6
+ input:
+ output: 23
+- setup:
+ cleanup:
+ args: 9
+ input:
+ output: 311
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: 773