From a7a5d5a80dae8b5243fdedf2cd62ff90b7996e99 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 7 Jun 2022 13:39:54 +0100 Subject: - Added solutions by Peter Campbell Smith. --- challenge-168/peter-campbell-smith/perl/ch-01.pl | 45 ------------------------ challenge-168/peter-campbell-smith/perl/ch-02.pl | 42 ---------------------- challenge-168/peter-campbell-smith/perl/ch-1.pl | 45 ++++++++++++++++++++++++ challenge-168/peter-campbell-smith/perl/ch-2.pl | 42 ++++++++++++++++++++++ 4 files changed, 87 insertions(+), 87 deletions(-) delete mode 100755 challenge-168/peter-campbell-smith/perl/ch-01.pl delete mode 100755 challenge-168/peter-campbell-smith/perl/ch-02.pl create mode 100755 challenge-168/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-168/peter-campbell-smith/perl/ch-2.pl (limited to 'challenge-168') diff --git a/challenge-168/peter-campbell-smith/perl/ch-01.pl b/challenge-168/peter-campbell-smith/perl/ch-01.pl deleted file mode 100755 index e82f952944..0000000000 --- a/challenge-168/peter-campbell-smith/perl/ch-01.pl +++ /dev/null @@ -1,45 +0,0 @@ -#!/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 deleted file mode 100755 index 3caf8583dc..0000000000 --- a/challenge-168/peter-campbell-smith/perl/ch-02.pl +++ /dev/null @@ -1,42 +0,0 @@ -#!/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); -} diff --git a/challenge-168/peter-campbell-smith/perl/ch-1.pl b/challenge-168/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..e82f952944 --- /dev/null +++ b/challenge-168/peter-campbell-smith/perl/ch-1.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-2.pl b/challenge-168/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..3caf8583dc --- /dev/null +++ b/challenge-168/peter-campbell-smith/perl/ch-2.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); +} -- cgit