diff options
Diffstat (limited to 'challenge-159')
| -rw-r--r-- | challenge-159/bruce-gray/raku/ch-1.raku | 25 | ||||
| -rw-r--r-- | challenge-159/bruce-gray/raku/ch-2.raku | 22 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-159/bruce-gray/raku/ch-1.raku b/challenge-159/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..635bf8c2d8 --- /dev/null +++ b/challenge-159/bruce-gray/raku/ch-1.raku @@ -0,0 +1,25 @@ +sub farey_sequence ( UInt $n --> Seq ) { + sub next_fs ( Rat $ab, Rat $cd --> Rat ) { + my $k = ($n + $ab.denominator) + div $cd.denominator; + + return ($k * $cd.numerator - $ab.numerator ) + / ($k * $cd.denominator - $ab.denominator); + } + + return 0/1, 1/$n, &next_fs ... 1/1; +} + + +my @tests = + 5 => (0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1), + 7 => (0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1), + 4 => (0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1), +; +use Test; +plan +@tests; +for @tests -> ( :key($in), :value(@expected) ) { + my @got = farey_sequence($in); + is-deeply @got, [@expected], "Farey Sequence $in"; + # put @got.map: *.nude.join('/'); +} diff --git a/challenge-159/bruce-gray/raku/ch-2.raku b/challenge-159/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..cf2a8085c3 --- /dev/null +++ b/challenge-159/bruce-gray/raku/ch-2.raku @@ -0,0 +1,22 @@ +use Prime::Factor; + +sub μ ( UInt $n ) { + my ( $prime_count, $not_square_free ) = ( +.elems, ?.repeated ) + given prime-factors($n); + + return $not_square_free ?? 0 !! ( (-1) ** $prime_count ); +} + + +my @tests = + 5 => -1, + 10 => 1, + 20 => 0, +; +use Test; +plan 1+@tests; +for @tests -> ( :key($in), :value($expected) ) { + is μ($in), $expected, "μ($in)"; +} +constant @A008683 = 1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0, 1, 0, -1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 1, -1, -1, 0, -1, 1, 0, 0, 1, -1; +is-deeply (1..78)».&μ , @A008683, "μ matches http://oeis.org/A008683 for 1..78"; |
