Last active
December 7, 2019 18:38
-
-
Save anmolj7/68cf7f0e3490a0b9b090c7e55c164c99 to your computer and use it in GitHub Desktop.
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
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