Created
April 30, 2017 04:44
-
-
Save ldong/557b2ed0b034b1db3fd98149a580e8e2 to your computer and use it in GitHub Desktop.
Gamma python version
This file contains 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
#!/usr/bin/env python | |
import math | |
def gamma(a): | |
# special case | |
if int(a) == 1: | |
return 0 | |
ind = (int)(math.floor(math.log(a*1.0)/math.log(2.0))) | |
first = ind * '1' + '0' | |
size = ((int)(math.pow(2, (int)(math.floor(math.log(a*1.0)/math.log(2.0)))))) | |
# print ind | |
# print "#0" + str(ind) +"b" | |
return first + (format(a%size, "0" + str(ind) +"b")) | |
for i in [4, 11, 47, 1, 202, 3, 2, 230, 8, 9]: | |
print i, gamma(i) |
Author
ldong
commented
Apr 30, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment