diff options
| author | Abigail <abigail@abigail.be> | 2020-12-07 14:16:02 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2020-12-07 14:16:02 +0100 |
| commit | e9e86b731cacc156b93266d997d7ff6615f695ff (patch) | |
| tree | ffc396208e50d2af95187af5be8df81cf147d981 | |
| parent | 60ae52346383e609bf9b551d2f4b6584285d394b (diff) | |
| download | perlweeklychallenge-club-e9e86b731cacc156b93266d997d7ff6615f695ff.tar.gz perlweeklychallenge-club-e9e86b731cacc156b93266d997d7ff6615f695ff.tar.bz2 perlweeklychallenge-club-e9e86b731cacc156b93266d997d7ff6615f695ff.zip | |
Perl solution for week 90/part 2
| -rw-r--r-- | challenge-090/abigail/perl/ch-2.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-090/abigail/perl/ch-2.pl b/challenge-090/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..ecb5170a17 --- /dev/null +++ b/challenge-090/abigail/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# Challenge +# +# Given two positive numbers $A and $B, demonstrate "Ethiopian +# Multiplication", using the given numbers. +# + +# +# "Ethiopian Multiplication" or "Ancient Egyption Multiplication" +# is nothing more than multiplication in binary. +# + +my $TICK = "\N{CHECK MARK}"; + +binmode STDOUT, ":utf8"; + +while (<>) { + use integer; + my ($A, $B) = /([0-9]+) \s+ ([0-9]+)/x or next; + my $w1 = length $A; + my $w2 = length (my $product = $A * $B); + while ($A) { + printf "%${w1}d %${w2}d %s\n" => $A, $B, $A % 2 ? $TICK : ""; + $A /= 2; + $B *= 2; + } + say " " x $w1, " ", "-" x $w2, " +"; + say " " x $w1, " ", $product; +} + + +__END__ |
