Bad Perl: Russian Peasant multiplication algorithm

Bad Perl: Russian Peasant multiplication algorithm

  • Comments 1

I found this programming contest interesting: here's what I've got.

perl -e "($a,$b)=@ARGV;map{$c+=$_*$b}grep{$a&$_}map{1<<$_}(0..log($a)/log 2);print$c" 7 19

I'm calling this a one-liner because the part between the quotes is less than 80 characters (75, to be exact.)  The full command line goes over :-(

Requires the inputs to be positive whole numbers.

Perfect example of Bad Perl.  Exercise: rewrite in Good Perl.

EDIT: got the whole thing down to 80 characters (with single-digit multiplicands.)

perl -e "($a,$b)=@ARGV;map{$c+=$b<<$_ if$a>>$_&1}(0..log($a)/log 2);print$c" 8 7

Leave a Comment
  • Please add 7 and 3 and type the answer here:
  • Post
  • I like the problem in programming contest, it is very interesting to solve that problem to me...but I need more time to study about pearl language...I hope so that I can solve that.

Page 1 of 1 (1 items)