def solution(start, length): checksum = 0 for i in range(length): base = start + length * i for j in range(length - i): checksum ^= base + j return checksum tests = [ ([0, 3], 2), ([17, 4], 14) ] for i, o in tests: result = solution(*i) print (i, result == o, result, o)