The answer to the question, which is marked as a duplicate, is incorrect and does not satisfy my needs.
My code is designed to calculate a hash from a series of numbers.
It’s easier to understand the structure in the form of a matrix. If I have 16 numbers, starting at 29, the structure will be: (start = 29, length = 4)
29, 30, 31, 32,
33, 34, 35, 36,
37, 38, 39, 40,
41, 42, 43, 44
This algorithm determines that the hash will be XOR numbers in bold:
29, 30, 31, 32, //,
33, 34, 35, //, 36,
37, 38, //, 39, 40,
41, //, 42, 43, 44
Hash = 29^30^31^32^33^34^35^37^38^39 = 54
My code is:
def answer(start, length): val=0 c=0 for i in range(length): for j in range(length): if j < length-i: val^=start+c c+=1 return val
The time required to compute large values such as answer(2000000000,10**4) is too long.
Limitations:
- Py2.7.6
- Only standard libraries except bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.
- Limited time to calculate.
Currently calculating test parameters (unknown to me) gives me a timeout error.
How can I improve the speed of my code for large values?