diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-07 13:30:33 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-07 13:30:33 +0100 |
| commit | 695339d6f95a4831e3652b8b7f67d9c43e61b22b (patch) | |
| tree | e762798b0c6aae4623a91c04ad10b7740f635e99 | |
| parent | 159d291ae61c81f60f14d537736492a3c4ef3352 (diff) | |
| parent | 1be4e73f09ca04b230f24f5a300c997e3d56f394 (diff) | |
| download | perlweeklychallenge-club-695339d6f95a4831e3652b8b7f67d9c43e61b22b.tar.gz perlweeklychallenge-club-695339d6f95a4831e3652b8b7f67d9c43e61b22b.tar.bz2 perlweeklychallenge-club-695339d6f95a4831e3652b8b7f67d9c43e61b22b.zip | |
Merge pull request #6218 from pjcs00/wk168
Week 168 submissions
| -rw-r--r-- | challenge-168/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-168/peter-campbell-smith/perl/ch-01.pl | 45 | ||||
| -rwxr-xr-x | challenge-168/peter-campbell-smith/perl/ch-02.pl | 42 |
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); +} |
