Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Formula with explanation solution in Clear category for Multiplication Table by brubru777
def checkio(first, second):
'''Returns the result of "Stephan's multiplication" as defined in the
problem.
'''
first_bin = bin(first)[2:]
ones = first_bin.count('1')
zeroes = len(first_bin) - ones
all_ones = 2 ** (len(bin(second)) - 2) - 1
# Computation details
# and_sum = zeroes * 0 + ones * second
# or_sum = zeroes * second + ones * all_ones
# xor_sum = zeroes * second + ones * (all_ones ^ second)
#
# Since n + (all_ones ^ n) = n for any number n, we get
return (zeroes * second + ones * all_ones) * 2
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(4, 6) == 38
assert checkio(2, 7) == 28
assert checkio(7, 2) == 18
Dec. 25, 2016
Comments: