[tpm] russian peasant multiplication

Dave Doyle dave.s.doyle at gmail.com
Wed Jul 22 08:40:34 PDT 2009


I just meant that ->{left} and ->{right} were easier to read with
regard to the  than ->[0] and ->[1] in the non-recursive version since
the algorithm description used left and right.

I do like the use of the bitwise & though. Nifty!

--
dave.s.doyle at gmail.com



On Wed, Jul 22, 2009 at 11:33 AM, Mike Stok<mike at stok.ca> wrote:
> I'm not sure why hash refs are easier than array refs, but you can use the
> trick that bit 0 of an integer is a conveninet multiplier to get rid of ? :
> if you like, and your integers don't overflow and get turned into floats.
>
> sub rpm {
>   my $left = abs(int(shift));
>   my $right = abs(int(shift));
>
>   return 0 if !$left || !$right;
>   return $right if $left == 1;
>   return (($left & 1) * $right) + rpm(int($left/2),$right*2);
> }
>
> I'd do
>
>
>
>
>
> Mike
>


More information about the toronto-pm mailing list