Skip to content

Instantly share code, notes, and snippets.

@marcoscastro
Created January 10, 2014 20:14
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 marcoscastro/8361751 to your computer and use it in GitHub Desktop.
Save marcoscastro/8361751 to your computer and use it in GitHub Desktop.
Russian peasant algorithm Goal: to multiply two numbers
#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