Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Last active December 7, 2019 18:38
Show Gist options
  • Save anmolj7/68cf7f0e3490a0b9b090c7e55c164c99 to your computer and use it in GitHub Desktop.
Save anmolj7/68cf7f0e3490a0b9b090c7e55c164c99 to your computer and use it in GitHub Desktop.
from math import log, floor
def dec_to_bin(N):
if N == 0 or N == 1:
return N
String = ''
Powers = []
while N > 0:
x = log(N, 2)
x = floor(x)
Powers.append(x)
N -= 1<<x
String = '0'*(max(Powers)+1)
String = list(String) #string is immutable
#So,
for x in Powers:
String[-x-1] = '1'
print(''.join(String))
def main():
dec_to_bin(int(input("Enter a number: ")))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment