Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I like the answer and anlysis by Artelius. Especially the proposal to use </p> <blockquote> <p>f(x,y) = x + y*K</p> </blockquote> <p>for some constant K is interesting and I'd like to just add a few more thoughts. What I'm doing here is not new, but very closely related to the <a href="http://www.brpreiss.com/books/opus4/html/page214.html" rel="noreferrer">Fibonacci hashing</a>, which I think has been proposed by Knuth.</p> <p>If we are using 64-bit integers then a collision f(x1, y1) = f(x2, y2) means</p> <blockquote> <p>0 = (dx + dy * K) mod 2<sup>64</sup>,</p> </blockquote> <p>where dx = x1 - x2 and dy = y1 - y2. This is the same as</p> <blockquote> <p>K = -dx*dy<sup>-1</sup> mod 2<sup>64</sup>,</p> </blockquote> <p>where dy<sup>-1</sup> is the modular inverse modulo 2<sup>64</sup>. If we want to choose the K such that f(x1, y1) != f(x2, y2) whenever the differences dx and dy are both small then we have to choose K such that </p> <blockquote> <p>K = -dx*dy<sup>-1</sup> mod 2<sup>64</sup>,</p> </blockquote> <p>has no solution such that both dx and dy are small. This can be achieved for example by choosing K close to phi * 2<sup>64</sup>, where phi = (sqrt(5)-1)/2 is the golden ratio. The golden ratio has a very special continued fraction expansion, i.e. in a certain sense it is a number that is hard to approximate well with a fraction.</p> <p>Hence, for 64-bit unsigned integers the following functions could be used</p> <blockquote> <p>f(x,y) = x + y * 11400714819323198485;</p> </blockquote> <p>or equivalently when using signed 64-bit integers</p> <blockquote> <p>f(x,y) = x - y * 7046029254386353131;</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload