diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-05 12:28:06 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-05 12:28:06 +0000 |
| commit | be2de97d4e7f1184125fd4452917e9f5ce6e8efa (patch) | |
| tree | 5a8d7bae2abfab84f560bdd5cfedefd5d01edfa2 | |
| parent | d1bbbea423cd523a516555aa3575049f44ca871b (diff) | |
| parent | 9118c387cefc7e86241e769a203d56ff39f43e87 (diff) | |
| download | perlweeklychallenge-club-be2de97d4e7f1184125fd4452917e9f5ce6e8efa.tar.gz perlweeklychallenge-club-be2de97d4e7f1184125fd4452917e9f5ce6e8efa.tar.bz2 perlweeklychallenge-club-be2de97d4e7f1184125fd4452917e9f5ce6e8efa.zip | |
Merge pull request #9195 from andemark/challenge-246
ch-2.raku do-over
| -rw-r--r-- | challenge-246/mark-anderson/raku/ch-2.raku | 40 |
1 files changed, 23 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..b411ffd00a 100644 --- a/challenge-246/mark-anderson/raku/ch-2.raku +++ b/challenge-246/mark-anderson/raku/ch-2.raku @@ -1,27 +1,33 @@ #!/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; - return False unless ($p1,$q1,$p2,$q2)>>.narrow.all ~~ Int; - return ($p1,$q1) eqv ($p2,$q2) + @a eqv (@a[0], @a[1], -> $a, $b { $a*$p + $b*$q }...*).head(5).Array } -sub p-and-q(@a, @b) +sub p(@a) +{ + given @a + { + .[2] / .[0] given .[0] >>*>> .[1;1] >>-<< .[1] >>*>> .[0;1] + } +} + +sub q(@a, $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 + { + (.[2] - .[0]*$p) / .[1] + } } |
