Skip to content

Instantly share code, notes, and snippets.

@jacknoble
Created November 22, 2013 05:17
Show Gist options
  • Select an option

  • Save jacknoble/7595215 to your computer and use it in GitHub Desktop.

Select an option

Save jacknoble/7595215 to your computer and use it in GitHub Desktop.
The second, worse version of the fast exponentiation.
def exp2(base, exponent)
return 1 if exponent == 0
if exponent.even?
even_recurse = exp2(base, exponent / 2)
even_recurse * even_recurse
else
odd_recurse = exp2(base, (exponent - 1) / 2)
base * (odd_recurse * odd_recurse)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment