The answer of the question this is marked duplicate of is wrong and does not satisfy my needs.
My code aims to calculate a hash from a series of numbers.
It is easier to understand the structure in the form of a matrix. If I have 16 numbers starting from 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
The given algorithm specifies the the hash will be the XOR of the numbers given 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^41=34
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 for large values like answer(2000000000,10**4) is way too much.
Constraints:
- Py2.7.6
- Only standard libraries except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.
- Limited time to compute.
Currently computing the test parameters (unknown to me) give me a timeout error.
How can the speed of my code be improved for bigger values?