diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-11 10:12:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-11 10:12:34 +0100 |
| commit | 3a904f6944c6c02f47ac408eb168de431242910c (patch) | |
| tree | a62a74619e635b302c5a63f8fa3208b9552f9ccc | |
| parent | 1c666818bcb3003fd293ae227540e35669499d27 (diff) | |
| parent | 8a75a2c18fb9c3ff3feaef7e34bd5b056dbc0ab3 (diff) | |
| download | perlweeklychallenge-club-3a904f6944c6c02f47ac408eb168de431242910c.tar.gz perlweeklychallenge-club-3a904f6944c6c02f47ac408eb168de431242910c.tar.bz2 perlweeklychallenge-club-3a904f6944c6c02f47ac408eb168de431242910c.zip | |
Merge pull request #6234 from PerlBoy1967/branch-for-challenge-168
w168 - Task 1 & 2
| -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; +} |
