Created
April 5, 2022 10:48
-
-
Save 2colours/999dc5b37ed348dbbaa66d99430d6479 to your computer and use it in GitHub Desktop.
Good möbius
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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