Created
June 13, 2013 15:09
-
-
Save gsamokovarov/5774472 to your computer and use it in GitHub Desktop.
Logarithmic integral exponent power implementation in Python
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 power_linear(number, exponent): | |
| return sum([number * number for _ in range(exponent)]) | |
| def power_log(number, exponent): | |
| if exponent == 0: | |
| return 1 | |
| half = power_log(number, exponent / 2) | |
| return half * half if exponent % 2 == 0 else number * half * half | |
| print power_log(2, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stack overflow error because
exponentnever reaches 0. (Python 3 turns the int (1) to float (0.5))exponent / 2should be replaced withexponent>>1in order to use as intended.