diff options
| -rw-r--r-- | challenge-047/noud/raku/ch-1.p6 | 62 | ||||
| -rw-r--r-- | challenge-047/noud/raku/ch-2.p6 | 14 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-047/noud/raku/ch-1.p6 b/challenge-047/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..45105fe235 --- /dev/null +++ b/challenge-047/noud/raku/ch-1.p6 @@ -0,0 +1,62 @@ +# Roman Calculator +# +# Write a script that accepts two roman numbers and operation. It should then +# perform the operation on the give roman numbers and print the result. +# +# For example, +# +# perl ch-1.pl V + VI +# +# It should print +# +# XI + + +my @roman-symbols = [ + 1_000, "M", + 900, "CM", + 500, "D", + 400, "CD", + 100, "C", + 90, "XC", + 50, "L", + 40, "CL", + 10, "X", + 9, "IX", + 5, "V", + 4, "IV", + 1, "I" +]; + +sub to-roman($i) { + for @roman-symbols -> $k, $v { + if ($i >= $k) { + return $v ~ to-roman($i - $k); + } + } + return ''; +} + +# It's assumed that the input is always a ROMAN number. No validation checks +# are done. +sub from-roman($s) { + for @roman-symbols -> $k, $v { + my $r = $s ~~ / ^$v(\w*) /; + if ($r) { + return $k + from-roman(Str($r[0])); + } + } + return 0; +} + +my %operators = + '+' => { $_[0] + $_[1] }, + '*' => { $_[0] * $_[1] }, + '-' => { $_[0] - $_[1] }, + '/' => { Int($_[0] / $_[1]) }, + '%' => { $_[0] % $_[1] }, +; + +sub MAIN($a, $op, $b) { + say to-roman(%operators{$op}((from-roman($a), from-roman($b)))); +} diff --git a/challenge-047/noud/raku/ch-2.p6 b/challenge-047/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..23d514564a --- /dev/null +++ b/challenge-047/noud/raku/ch-2.p6 @@ -0,0 +1,14 @@ +# Gapful Number +# +# Write a script to print first 20 Gapful Numbers greater than or equal to 100. +# Please check out the page for more information about Gapful Numbers. +# +# Gapful numbers >= 100: numbers that are divisible by the number formed by +# their first and last digit. Numbers up to 100 trivially have this property +# and are excluded. + +my @gapful = (100..Inf).grep({ + $_ % Int(Str($_).comb[0] ~ Str($_).comb[*-1]) == 0 +}); + +@gapful[^20].say; |
