aboutsummaryrefslogtreecommitdiff
path: root/challenge-168
diff options
context:
space:
mode:
authorRick Bychowski <rick@hiranyaloka.com>2022-06-12 15:33:14 -0700
committerRick Bychowski <rick@hiranyaloka.com>2022-06-12 15:33:14 -0700
commit3865602423922dfe292a0e42bf7305f4c0bdc119 (patch)
treeae5e7e55cfc91b74dbe26107cd3322258384b63e /challenge-168
parentb2a12ebba2b4df73093074442da27eb43dcbb73b (diff)
downloadperlweeklychallenge-club-3865602423922dfe292a0e42bf7305f4c0bdc119.tar.gz
perlweeklychallenge-club-3865602423922dfe292a0e42bf7305f4c0bdc119.tar.bz2
perlweeklychallenge-club-3865602423922dfe292a0e42bf7305f4c0bdc119.zip
ch-168; Perrin Prime and Home Prime
Diffstat (limited to 'challenge-168')
-rwxr-xr-xchallenge-168/rick-bychowski/raku/ch-1.raku30
-rwxr-xr-xchallenge-168/rick-bychowski/raku/ch-2.raku51
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-168/rick-bychowski/raku/ch-1.raku b/challenge-168/rick-bychowski/raku/ch-1.raku
new file mode 100755
index 0000000000..6eb4ca7679
--- /dev/null
+++ b/challenge-168/rick-bychowski/raku/ch-1.raku
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+
+#`{
+Task 1: Perrin Prime - Calculate the first 13 Perrin Primes.
+
+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.
+
+EXAMPLE
+f(13) = [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977]
+}
+
+sub MAIN(Int $n) {
+ say "f($n) = ", pp($n);
+}
+
+sub pp(Int $n) {
+ my @seed = <3 0 2>;
+ my %pp;
+ for 1 .. * -> $i {
+ my $ppn = @seed[*-3] + @seed[*-2];
+ @seed.push($ppn);
+ %pp{$ppn} = 1 if $ppn.is-prime;
+ last if %pp.elems == $n;
+ }
+ my @pp = %pp.keys.map({.Int}).sort;
+}
+
diff --git a/challenge-168/rick-bychowski/raku/ch-2.raku b/challenge-168/rick-bychowski/raku/ch-2.raku
new file mode 100755
index 0000000000..4056fcdcb7
--- /dev/null
+++ b/challenge-168/rick-bychowski/raku/ch-2.raku
@@ -0,0 +1,51 @@
+#!/usr/bin/env raku
+
+#`{
+Task 2: HOME 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.
+
+REFERENCE
+https://en.wikipedia.org/wiki/Home_prime
+https://oeis.org/A037274
+
+EXAMPLE
+HP(10) = 773, as
+ 10 factors as 2×5 yielding HP10(1) = 25,
+ 25 factors as 5×5 yielding HP10(2) = HP25(1) = 55,
+ 55 = 5×11 implies HP10(3) = HP25(2) = HP55(1) = 511,
+ 511 = 7×73 gives HP10(4) = HP25(3) = HP55(2) = HP511(1) = 773, a prime number.
+
+}
+
+sub MAIN(Int $n where $n > 1) {
+ say factor($n);
+}
+
+sub factor(Int $n, $hpn = '') {
+ my Int $f1;
+ my Int $f2;
+ my $hp = $hpn;
+ for 2 ..^ $n -> $i {
+ if $n %% $i {
+ $f1 = $i;
+ $f2 = Int($n/$i);
+ last;
+ }
+ }
+ if $f1.defined {
+ $hp ~= $f1;
+ if $f2.is-prime {
+ $hp ~= $f2;
+ factor(Int($hp));
+ } else {
+ factor($f2, $hp);
+ }
+ } else {
+ return $n;
+ }
+}