Created
April 6, 2014 16:25
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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