From 9139b17f11809f94217c245e976acc7c0071423d Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 4 Dec 2023 23:08:28 +0000 Subject: ch-2.raku do-over --- challenge-246/mark-anderson/raku/ch-2.raku | 43 ++++++++++++++++++------------ 1 file 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] + } } -- cgit From b334f142aee1196cad0c3a1a4a4ea4e3df06b643 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 4 Dec 2023 23:13:15 +0000 Subject: ch-2.raku do-over --- challenge-246/mark-anderson/raku/ch-2.raku | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/challenge-246/mark-anderson/raku/ch-2.raku b/challenge-246/mark-anderson/raku/ch-2.raku index 569d971ed1..97aef3e5a6 100644 --- a/challenge-246/mark-anderson/raku/ch-2.raku +++ b/challenge-246/mark-anderson/raku/ch-2.raku @@ -13,9 +13,7 @@ sub linear-recurrence-of-second-order(@a) return False unless all($p, $q) ~~ Int; - my @s = (@a[0], @a[1], -> $a, $b { $a*$p + $b*$q }...*).head(5).Array; - - @a eqv @s + @a eqv (@a[0], @a[1], -> $a, $b { $a*$p + $b*$q }...*).head(5).Array } sub p(@a) -- cgit From 9118c387cefc7e86241e769a203d56ff39f43e87 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 4 Dec 2023 23:29:15 +0000 Subject: ch-2.raku do-over --- challenge-246/mark-anderson/raku/ch-2.raku | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/challenge-246/mark-anderson/raku/ch-2.raku b/challenge-246/mark-anderson/raku/ch-2.raku index 97aef3e5a6..b411ffd00a 100644 --- a/challenge-246/mark-anderson/raku/ch-2.raku +++ b/challenge-246/mark-anderson/raku/ch-2.raku @@ -24,11 +24,10 @@ sub p(@a) } } -sub q(@a is copy, $p) +sub q(@a, $p) { given @a { - .[0] *= $p; - (.[2] - .[0]) / .[1] + (.[2] - .[0]*$p) / .[1] } } -- cgit