RosettaCodeData/Task/Stern-Brocot-sequence/Perl-6/stern-brocot-sequence.pl6
2017-09-25 22:28:19 +02:00

11 lines
301 B
Raku
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

constant @Stern-Brocot = 1, 1, {
|(@_[$_ - 1] + @_[$_], @_[$_]) given ++$
} ... *;
 
say @Stern-Brocot[^15];
 
for (flat 1..10, 100) -> $ix {
say "first occurrence of $ix is at index : ", 1 + @Stern-Brocot.first($ix, :k);
}
 
say so 1 == all map ^1000: { [gcd] @Stern-Brocot[$_, $_ + 1] }