Skip to content

Instantly share code, notes, and snippets.

@tyler
Created May 27, 2010 09:37
Show Gist options
  • Save tyler/415635 to your computer and use it in GitHub Desktop.
Save tyler/415635 to your computer and use it in GitHub Desktop.
# Iterative O(log n) exponentiation
def fast_expt(b, n)
a = 1
while n > 0
if even?(n)
n /= 2
b *= b
else
n -= 1
a *= b
end
end
a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment