Skip to content

Instantly share code, notes, and snippets.

@2colours
Created April 5, 2022 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2colours/999dc5b37ed348dbbaa66d99430d6479 to your computer and use it in GitHub Desktop.
Save 2colours/999dc5b37ed348dbbaa66d99430d6479 to your computer and use it in GitHub Desktop.
Good möbius
#!/usr/bin/env raku
constant \minus-one = -1;
multi möbius(1 --> 1) {}
multi möbius($n where { .is-prime } --> minus-one) {}
multi möbius($n) {
my $divisor = (2..^$n).first($n %% *);
my $quotient = $n div $divisor;
$quotient %% $divisor ?? 0 !! möbius($divisor) * möbius($quotient)
}
my $n = prompt 'Input: $n = ';
say "Output: {möbius($n)}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment