Created
November 22, 2013 05:17
-
-
Save jacknoble/7595215 to your computer and use it in GitHub Desktop.
The second, worse version of the fast exponentiation.
This file contains hidden or 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
| 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