4 ms·
This is very nice, but I can see a way to optimize it even further. Consider pi[i:i+10] and pi[i+1:i+11] they are highly similar. Intuitively it seems that sto
by im3w1l 2y ago
This is very nice, but I can see a way to optimize it even further.
Consider pi[i:i+10] and pi[i+1:i+11] they are highly similar. Intuitively it seems that storing them both seems wasteful. But how can we avoid storing them both? This is where we have to get clever.
We are looking for a length 18 match. If position a and position b are a length 18 match, then it holds that (a,b) is a length 10 match. (a+1,b+1) is also a length 10 match, and so is (a+2,b+2). Wouldn't it be nice if we could store only one of a,a+1,a+2 etc?
The naive attempt is to do the loop with a stride of say 3, but this will not work as it may happen that e.g. a=1 mod 3 and b=2 mod 3.
However there is another approach that does work. We need to use a position-independent way of deciding between a,a+1 etc. We can do it thusly. Loop over all the numbers. When we encounter position a we score the length 10 sequences starting at a, a+1.. a+8. Any scoring function will do as long as it doesn't have ties. For simplicity we can score=number. The highest scoring number is stored. As an implementation detail we can use a "sliding window maximum" algorithm to avoid recomputing and recomparing the scores.
Note that in this scheme we may actually end up storing more than one of a, ..a+8. But it will cut down the storage quite a bit at least.
- sponaugle 2y agoI need to noodle on this. Hmm.
- kstrauser 2y agoThat would be O(n^2) though, where n is the max length of the strings to compare, right?
- sponaugle 2y agoMy current method is O( (n/h)^2 ) where h is the hash size, which is really O(n^2) for n>>h. I'll have to think about this to see if it would be better.
- im3w1l 2y agoNo, it's possible to do much better https://www.geeksforgeeks.org/sliding-window-maximum-maximum-of-all-subarrays-of-size-k/ https://www.geeksforgeeks.org/sliding-window-maximum-maximum... Edit: It should also be mentioned that due to the conjectured normality of pi, comparing two sequences will on average terminate very quickly, just looking at one or two digits will be enough most of the time, so a comparison is expected O(1).
- sponaugle 2y agoIndeed, the comparison length is 90% one hit, 99% two hits, etc.