# Prints out the representation of a nonnegative number in base 2,
# using two recursive procedures, one of which is tail-recursive.
#
# Written by Eric Martin for COMP9021
def print_binary_representation_1(n):
_print_binary_representation_1(n)
print()
def _print_binary_representation_1(n):
if n >= 2:
_print_binary_representation_1(n // 2)
print(n % 2, end = '')
def print_binary_representation_2(n):
if n == 0:
print(0)
else:
_print_binary_representation_2(n, n.bit_length() - 1)
print()
def _print_binary_representation_2(n, exp):
if exp < 0:
return
if 2 ** exp <= n:
print(1, end = '')
_print_binary_representation_2(n - 2 ** exp, exp - 1)
else:
print(0, end = '')
_print_binary_representation_2(n, exp - 1)
if __name__ == '__main__':
print('Printing out 0, 1, 2, 5 and 23 in binary, first method:')
print_binary_representation_1(0)
print_binary_representation_1(1)
print_binary_representation_1(2)
print_binary_representation_1(5)
print_binary_representation_1(23)
print()
print('Printing out 0, 1, 2, 5 and 23 in binary, second method:')
print_binary_representation_2(0)
print_binary_representation_2(1)
print_binary_representation_2(2)
print_binary_representation_2(5)
print_binary_representation_2(23)
Resource created Wednesday 02 September 2015, 11:06:53 AM.
file: printing_to_binary.py