Created
March 10, 2015 18:26
-
-
Save grondilu/b44db2a1af91e736382a to your computer and use it in GitHub Desktop.
alternate implementation of base-repeating
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
use MONKEY_TYPING; | |
augment class Rat { | |
method my-base-repeating($base = 10) { | |
return ~self, '' if self.narrow ~~ Int; | |
my (@quotients, @remainders, %remainders); | |
push @quotients, [div] my ($nu, $de) = self.nude; | |
loop { | |
push @remainders, $nu %= $de; | |
last if %remainders{$nu}++ or $nu == 0; | |
$nu *= $base; | |
push @quotients, $nu div $de; | |
} | |
@quotients.=map(*.base($base)); | |
my @cycle = $nu ?? splice(@quotients, @remainders.first-index($nu) + 1) !! (); | |
splice @quotients, 1, 0, '.'; | |
return @quotients.join, @cycle.join; | |
} | |
} | |
say (1/$_).my-base-repeating().perl for 1..10; | |
# vim: ft=perl6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had written something like that for a project Euler problem, and I thought it was simpler than the implementation in rakudo's core. Not sure why there is a 10000 limitation in rakudo's implementation.