Last active
October 24, 2019 12:12
-
-
Save wkta/e66f115ea473041bae551caa999e8630 to your computer and use it in GitHub Desktop.
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
PROMPT_MSG='type an integer greater or equal to 1 > ' | |
input_ok = False | |
while not input_ok: | |
try: | |
num_tested = int(input(PROMPT_MSG)) | |
if num_tested >= 1: | |
input_ok = True | |
except ValueError: | |
pass | |
print(num_tested) | |
n_max = 0 | |
bsup = 1 | |
while num_tested > bsup: | |
n_max += 1 | |
bsup = 1<<n_max | |
print("= ", end='') | |
is_first_print = True | |
total = 0 | |
for n in range(bsup): | |
prod = num_tested&(1<<n); | |
if prod > 0: | |
if is_first_print: | |
is_first_print=False | |
else: | |
print('+', end='') | |
print(str(prod), end='') | |
total += prod | |
if total == num_tested: | |
break | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment