Created
January 6, 2013 14:55
-
-
Save masak/4467722 to your computer and use it in GitHub Desktop.
How to double numbers left-to-right
This file contains hidden or 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 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