Created
January 10, 2014 20:14
-
-
Save marcoscastro/8361751 to your computer and use it in GitHub Desktop.
Russian peasant algorithm
Goal: to multiply two numbers
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
#include <stdio.h> | |
int russian_peasant(unsigned int x, unsigned int y) | |
{ | |
int resultado = 0; | |
while(y > 0) | |
{ | |
if(y % 2 != 0) | |
resultado += x; | |
x *= 2; | |
y /= 2; | |
} | |
return resultado; | |
} | |
int main() | |
{ | |
printf("%d\n", russian_peasant(2, 3)); | |
printf("%d\n", russian_peasant(10, 3)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment