aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-168/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-168/peter-campbell-smith/perl/ch-01.pl45
-rwxr-xr-xchallenge-168/peter-campbell-smith/perl/ch-02.pl42
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-168/peter-campbell-smith/blog.txt b/challenge-168/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..0a6ae9b139
--- /dev/null
+++ b/challenge-168/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2022/06/more-funny-numbers-and-very-big-one.html
diff --git a/challenge-168/peter-campbell-smith/perl/ch-01.pl b/challenge-168/peter-campbell-smith/perl/ch-01.pl
new file mode 100755
index 0000000000..e82f952944
--- /dev/null
+++ b/challenge-168/peter-campbell-smith/perl/ch-01.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-06-06
+# PWC 168 task 1
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+use Math::Prime::Util 'is_prime';
+use Math::BigInt lib => 'GMP';
+
+# 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]
+
+# Blog at https://pjcs-pwc.blogspot.com/2022/06/more-funny-numbers-and-very-big-one.html
+
+my (@perrin, $j, $result, $count);
+
+# initialise
+@perrin = (3, 0, 2);
+$result = qq[, 2, 3, ];
+$count = 2;
+
+# calculate perrin primes until 13 unique ones found
+for $j (3 .. 9999) {
+
+ # get next term
+ $perrin[$j] = Math::BigInt->new($perrin[$j - 2]);
+ $perrin[$j]->badd($perrin[$j - 3]);
+
+ # no good unless prime and not already seen
+ next unless is_prime($perrin[$j]);
+ next if $result =~ m|, $perrin[$j],|;
+
+ # add to results
+ $result .= qq[$perrin[$j], ];
+ last if $count ++ == 12;
+}
+
+# deliver the answer
+say qq[f($count) = ] . substr($result, 2, -2);
diff --git a/challenge-168/peter-campbell-smith/perl/ch-02.pl b/challenge-168/peter-campbell-smith/perl/ch-02.pl
new file mode 100755
index 0000000000..3caf8583dc
--- /dev/null
+++ b/challenge-168/peter-campbell-smith/perl/ch-02.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-06-06
+# PWC 168 task 2
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+use Math::Prime::Util qw[factor is_prime];
+
+# 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.
+
+# Blog at https://pjcs-pwc.blogspot.com/2022/06/more-funny-numbers-and-very-big-one.html
+
+my ($test, $hp);
+
+# loop over 2 to 48 (49 is known to be difficult!)
+for $test (2 .. 48) {
+ $hp = home($test);
+ say qq[hp($test) = $hp];
+}
+
+sub home {
+
+ my (@prime_factors, $concat);
+
+ # get prime factors
+ @prime_factors = factor(shift);
+
+ # concatenate them
+ $concat .= $_ for @prime_factors;
+
+ # either we have an answer or need to go round again
+ return $concat if is_prime($concat);
+ home($concat);
+}