diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-11 10:13:58 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-11 10:13:58 +0100 |
| commit | 9a85311c82f1a21d9614daca3f0fc8c439f849b3 (patch) | |
| tree | fd0839b0dc5964b6795954755735ba1824f38c3a | |
| parent | bcdf1cca038a3b64d16f48a5c12c4289c976d6f2 (diff) | |
| parent | 3a904f6944c6c02f47ac408eb168de431242910c (diff) | |
| download | perlweeklychallenge-club-9a85311c82f1a21d9614daca3f0fc8c439f849b3.tar.gz perlweeklychallenge-club-9a85311c82f1a21d9614daca3f0fc8c439f849b3.tar.bz2 perlweeklychallenge-club-9a85311c82f1a21d9614daca3f0fc8c439f849b3.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rwxr-xr-x | challenge-168/perlboy1967/perl/ch-1.pl | 51 | ||||
| -rwxr-xr-x | challenge-168/perlboy1967/perl/ch-2.pl | 47 |
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-168/perlboy1967/perl/ch-1.pl b/challenge-168/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..50073d073d --- /dev/null +++ b/challenge-168/perlboy1967/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 168 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-168/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +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] + +=cut + +use v5.16; +use warnings; + +use Math::Prime::XS qw(is_prime); + +# Prototype(s) +sub perrin ($); + +my %p; +my ($n,$p) = (0,1); +while ($n < 13) { + my $pN = perrin($p++); + if (is_prime($pN) && !exists $p{$pN}) { + say $pN; $p{$pN}++; $n++; + } +} + +sub perrin ($) { + my ($n) = @_; + + state $p = [3, 0, 2]; + + return $p->[$n] if defined $p->[$n]; + + $p->[@$p] = $p->[@$p-2] + $p->[@$p-3] while (!defined $p->[$n]); + + return $p->[$n]; +} diff --git a/challenge-168/perlboy1967/perl/ch-2.pl b/challenge-168/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..6f710f9110 --- /dev/null +++ b/challenge-168/perlboy1967/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 168 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-168/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.16; +use warnings; + +use Math::Prime::XS qw(is_prime); +use Math::Factor::XS qw(prime_factors); +use Try::Tiny; + +# prototype(s) +sub homePrime ($); + +for my $n (2 .. 1000) { + my $h = homePrime($n); + printf "%d\t=> %s\n", $n, (!defined $h ? 'Too big to handle' : $h); +} + +sub homePrime ($) { + my ($n) = @_; + + try { $n = join '', prime_factors($n) while (!is_prime($n)) } + catch { return }; + + return $n; +} |
