Skip to content

Instantly share code, notes, and snippets.

@wkta
Last active October 24, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkta/e66f115ea473041bae551caa999e8630 to your computer and use it in GitHub Desktop.
Save wkta/e66f115ea473041bae551caa999e8630 to your computer and use it in GitHub Desktop.
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