Skip to content

Instantly share code, notes, and snippets.

@sealfin
Created April 6, 2014 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sealfin/10008299 to your computer and use it in GitHub Desktop.
Save sealfin/10008299 to your computer and use it in GitHub Desktop.
Supposedly, Russian peasants perform multiplication in the following fashion: they write the two numbers which are to be multiplied at the top of two columns; then, they multiply the number in the right column by two, and divide the number in the left column by two – throwing away any fractional part – continuing until the number in the left col…
public class RussianPeasantMultiplication
{
public static int multiply( int l, int r )
{
int result = 0, lsb = l & 1;
if( lsb == 1 )
result += r;
while( l != 0 )
{
r <<= 1;
l >>= 1;
lsb = l & 1;
if( lsb == 1 )
result += r;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment