Building on @JosefZ's answer, I've analyzed the sequence of row values (and it's the same for column values) and found a cool pattern in the differences between consecutive terms. Starting from the third term, the differences between consecutive terms follow this sequence:
By taking the base-2 logarithm of these differences, we get the sequence of values:
What stands out is that the logarithmic value 7
appears once, 6
appears twice, 5
appears four times, and so on. In general, for a difference of (2^n), it appears (2^{7-n}) times.
To quantify this, I derived a formula for the difference between the (n)-th and ((n-1))-th terms of the sequence:
difference = math.pow(2, 7 - math.floor(math.log2(n - 2)))
Then, I created a function to generate a mapping of the sequence using this formula:
import math
def generate_mapping(n: int) -> dict:
mapping = {0: 1, 16256: 2}
tmp = 16256
for i in range(3, n + 1):
tmp += math.pow(2, 7 - math.floor(math.log2(i - 2)))
mapping[tmp] = i
return mapping
To optimize this, we can replace the expression math.pow(2, 7 - math.floor(math.log2(n - 2)))
with a bit-shift operation:
1 << (7 - (i - 2).bit_length() + 1)
This reduces computation time by avoiding floating-point operations. Here's the updated version of the function with the optimization:
def generate_mapping(n: int) -> dict:
mapping = {0: 1, 16256: 2}
tmp = 16256
for i in range(3, n + 1):
tmp += 1 << (7 - (i - 2).bit_length() + 1)
mapping[tmp] = i
return mapping
As you can see, the latter is much faster though this kind of optimization may be needless.
After applying the optimized mapping, I get the following result:
As you can see, the strange row and column values have been successfully converted into indexes.