Skip to content

Instantly share code, notes, and snippets.

@grondilu
Created March 10, 2015 18:26
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 grondilu/b44db2a1af91e736382a to your computer and use it in GitHub Desktop.
Save grondilu/b44db2a1af91e736382a to your computer and use it in GitHub Desktop.
alternate implementation of base-repeating
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
@grondilu
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment