Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recommend using <strong>92821</strong>. Here's why.</p> <p>To give a meaningful answer to this you have to know something about the possible values of <code>i</code> and <code>j</code>. The only thing I can think of in general is, that in many cases small values will be more common than large values. (The odds of 15 appearing as a value in your program are much better than, say, 438281923.) So it seems a good idea to make the smallest hashcode collision as large as possible by choosing an appropriate prime. For 31 this rather bad - already for <code>i=-1</code> and <code>j=31</code> you have the same hash value as for <code>i=0</code> and <code>j=0</code>.</p> <p>Since this is interesting, I've written a little program that searched the whole int range for the best prime in this sense. That is, for each prime I searched for the minimum value of <code>Math.abs(i) + Math.abs(j)</code> over all values of <code>i,j</code> that have the same hashcode as <code>0,0</code>, and then took the prime where this minimum value is as large as possible. </p> <p><em>Drumroll</em>: the best prime in this sense is 486187739 (with the smallest collision being <code>i=-25486, j=67194</code>). Nearly as good and much easier to remember is 92821 with the smallest collision being <code>i=-46272 and j=46016</code>.</p> <p>If you give "small" another meaning and want to be the minimum of <code>Math.sqrt(i*i+j*j)</code> for the collision as large as possible, the results are a little different: the best would be 1322837333 with <code>i=-6815 and j=70091</code>, but my favourite 92821 (smallest collision <code>-46272,46016</code>) is again almost as good as the best value.</p> <p>I do acknowledge that it is quite debatable whether these calculation make much sense in practice. But I do think that taking 92821 as prime makes much more sense than 31, unless you have good reasons not to.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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