aboutsummaryrefslogtreecommitdiff
path: root/challenge-246
diff options
context:
space:
mode:
authorMark <53903062+andemark@users.noreply.github.com>2023-12-04 23:08:28 +0000
committerMark <53903062+andemark@users.noreply.github.com>2023-12-04 23:08:28 +0000
commit9139b17f11809f94217c245e976acc7c0071423d (patch)
tree401530056a24d59bf1b264d3142450e6d1d4fb9a /challenge-246
parent6487a92e723db88c4c72a2cc5a9e569a22f9a019 (diff)
downloadperlweeklychallenge-club-9139b17f11809f94217c245e976acc7c0071423d.tar.gz
perlweeklychallenge-club-9139b17f11809f94217c245e976acc7c0071423d.tar.bz2
perlweeklychallenge-club-9139b17f11809f94217c245e976acc7c0071423d.zip
ch-2.raku do-over
Diffstat (limited to 'challenge-246')
-rw-r--r--challenge-246/mark-anderson/raku/ch-2.raku43
1 files changed, 26 insertions, 17 deletions
diff --git a/challenge-246/mark-anderson/raku/ch-2.raku b/challenge-246/mark-anderson/raku/ch-2.raku
index d162ab93d3..569d971ed1 100644
--- a/challenge-246/mark-anderson/raku/ch-2.raku
+++ b/challenge-246/mark-anderson/raku/ch-2.raku
@@ -1,27 +1,36 @@
#!/usr/bin/env raku
-use Math::Matrix;
use Test;
-# Disclaimer: This might be totally wrong but it seems right.
+ok linear-recurrence-of-second-order([1,1,2,3,5]);
+nok linear-recurrence-of-second-order([4,2,4,5,7]);
+ok linear-recurrence-of-second-order([4,1,2,-3,8]);
-ok task2(1,1,2,3,5);
-nok task2(4,2,4,5,7);
-ok task2(4,1,2,-3,8);
-
-sub task2(*@a)
+sub linear-recurrence-of-second-order(@a)
{
- my @equations = @a.rotor(3 => -2);
-
- my ($p1,$q1) = p-and-q(@equations[0], @equations[1]);
- my ($p2,$q2) = p-and-q(@equations[1], @equations[2]);
+ my @eqn = @a.rotor(3 => -2).head(2);
+ my $p = p(@eqn).narrow;
+ my $q = q(@eqn.pop, $p).narrow;
+
+ return False unless all($p, $q) ~~ Int;
+
+ my @s = (@a[0], @a[1], -> $a, $b { $a*$p + $b*$q }...*).head(5).Array;
- return False unless ($p1,$q1,$p2,$q2)>>.narrow.all ~~ Int;
- return ($p1,$q1) eqv ($p2,$q2)
+ @a eqv @s
}
-sub p-and-q(@a, @b)
+sub p(@a)
+{
+ given @a
+ {
+ .[2] / .[0] given .[0] >>*>> .[1;1] >>-<< .[1] >>*>> .[0;1]
+ }
+}
+
+sub q(@a is copy, $p)
{
- my $A = Math::Matrix.new([[@a.head(2), @b.head(2)]]);
- my $B = Math::Matrix.new([[@a.tail], [@b.tail]]);
- |$A.inverted.dot-product($B)
+ given @a
+ {
+ .[0] *= $p;
+ (.[2] - .[0]) / .[1]
+ }
}