diff options
| -rw-r--r-- | challenge-168/cheok-yin-fung/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-168/cheok-yin-fung/perl/ch-2.pl | 59 |
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-168/cheok-yin-fung/perl/ch-1.pl b/challenge-168/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..61ba32d69e --- /dev/null +++ b/challenge-168/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +# The Weekly Challenge 168 +# Task 1 Perrin Prime +use v5.24.0; +use warnings; +use List::Util qw/reduce none/; +use Math::BigInt::GMP; # [remark] +use Math::BigInt::Pari; # [remark] +use Math::Prime::Util::GMP qw/is_prime next_prime/; +use bigint try => 'GMP,Pari'; # [remark] +# remark: follow suggestions on POD of Math::Prime::Util + +my @perrin_primes = (2,3); +my ($ppnm3,$ppnm2,$ppnm1, $ppn) = (3,0,2,3); + +while (scalar @perrin_primes < 13) { + if (is_prime($ppn) == 2) { + push @perrin_primes, $ppn if none {$_ == $ppn} @perrin_primes; + say $ppn; + } + $ppnm3 = $ppnm2; + $ppnm2 = $ppnm1; + $ppnm1 = $ppn; + $ppn = $ppnm2 + $ppnm3; +} + +say join ", ", @perrin_primes; + +# time: +# real 0m0.078s +# user 0m0.056s +# sys 0m0.008s + diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..0518d1dbcd --- /dev/null +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# The Weekly Challenge 168 +# Task 2 Home Prime +# Usage: ch-2.pl [integer > 1] +use v5.24.0; +use warnings; +use List::Util qw/reduce/; +use Math::BigInt::GMP; # [remark] +use Math::BigInt::Pari; # [remark] +use Math::Prime::Util::GMP qw/is_prime next_prime/; +use bigint try => 'GMP,Pari'; # [remark] +# remark: follow suggestions on POD of Math::Prime::Util + + +say hp($ARGV[0]) if defined($ARGV[0]); + + + +sub hp { + my_hp($_[0],0); +} + + + +sub my_hp { + my $recur_depth = $_[1]; + + die "Walk so far but still cannot get result. :(\n" if $recur_depth > 10; + my $num = $_[0]; + if (is_prime($num) == 2) { + return $num; + } + + my @factors = (); + my $p = 2; + do { + if (!($num % $p)) { + push @factors, $p; + $num /= $p; + } + else { + $p = next_prime($p); + } + } while ($num != 1); + my $nxt = (reduce { $a . $b } @factors); + say " $nxt"; + return my_hp($nxt, ++$recur_depth); +} + +use Test::More tests => 5; +# test data from OEIS +ok hp(10) == 773; +ok hp(24) == 331_319; +ok hp(32) == 241_271; +ok hp(45) == 3_411_949; + +ok hp(44) == 22815088913; # this test spends approx 0.5 min (on CY's laptop). + +# ok hp(40) == 3314192745739; # this test spends approx. 2 min. |
