Skip to content

Instantly share code, notes, and snippets.

@masak
Created January 6, 2013 14:55
Show Gist options
  • Save masak/4467722 to your computer and use it in GitHub Desktop.
Save masak/4467722 to your computer and use it in GitHub Desktop.
How to double numbers left-to-right
use Test;
sub double($n) {
my @digits = $n.comb;
my @result;
my $last;
for @digits -> $d {
my $dd = $d * 2;
if $dd >= 10 {
$last++;
$dd -= 10;
}
push @result, $last if defined $last;
$last = $dd;
}
push @result, $last;
return @result.join;
}
is double(1), 2;
is double(19), 38;
is double(1048576), 2097152;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment