Skip to content

Instantly share code, notes, and snippets.

@gsamokovarov
Created June 13, 2013 15:09
Show Gist options
  • Save gsamokovarov/5774472 to your computer and use it in GitHub Desktop.
Save gsamokovarov/5774472 to your computer and use it in GitHub Desktop.
Logarithmic integral exponent power implementation in Python
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)
@SteveHere
Copy link

SteveHere commented Jul 8, 2020

Stack overflow error because exponent never reaches 0. (Python 3 turns the int (1) to float (0.5))

exponent / 2 should be replaced with exponent>>1 in order to use as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment