Hong-Phuc Bui
2024-05-15 7df20ac74520ea7cbb38152e14f8bc4e8fb125e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#! /usr/bin/env python
 
from typing import Final
import sys
 
n = int(sys.argv[1])
power = 1
LIMIT: Final[int] = n // 2
 
# find the largest power of 2, which is less than or equal to n/2
while power <= LIMIT:
    power *= 2
 
binary_presentation = ""
while power > 0:
    if n < power:
        binary_presentation += "0"
    else:
        binary_presentation += "1"
        n -= power
    power //= 2
print(binary_presentation)