Skip to content

Instantly share code, notes, and snippets.

@zapstar
Created April 21, 2014 16:41
Show Gist options
  • Save zapstar/11148239 to your computer and use it in GitHub Desktop.
Save zapstar/11148239 to your computer and use it in GitHub Desktop.
A method for multiplying faster than our repeated addition
# Really cool algorithm I learnt from a blog somewhere
# Also sometimes called russian's peasents algorithm
def fast_multiply(a,b):
z=0;
while a > 0:
if a % 2 == 1:
z = z + b
a = a >> 1
b = b << 1
return z
print fast_multiply(14,11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment