title: Unexpected arithmetic results using Hashset<long>
body: <p>I'm generating three integer sequences that I store in hashsets called <code>Triangle</code>, <code>Pentagon</code>, <code>Hexagon</code>. The sequences themselves are based on a simple arithmetic formula and are monotonically increasing.</p>
  
  <p>I haven't analysed the resulting hashset <code>Triangle</code> beyond the third term, because that's where the problems start.</p>
  
  <pre><code>    public static long TriPentHex(int n)
      {
  
          HashSet&lt;long&gt; Triangle = new HashSet&lt;long&gt;();
          List&lt;long&gt; TriList = new List&lt;long&gt;();
          long temp = 0;
          for (int i = 1; i &lt;= n; i++)
          {
              temp = (i * (i + 1)) / 2;
              Triangle.Add(temp);
              TriList.Add(temp);
          }
          HashSet&lt;long&gt; Pentagon = new HashSet&lt;long&gt;();
          for (int i = 1; i &lt;= n; i++)
          {
              temp = i * (3 * i - 1) / 2;
              if (temp == 4128501)
                  Debug.WriteLine(temp);
              Pentagon.Add(temp);
          }
          HashSet&lt;long&gt; Hexagon = new HashSet&lt;long&gt;();
          for (int i = 1; i &lt;= n; i++)
              Hexagon.Add(i * (2 * i - 1));
  
          Triangle.IntersectWith(Pentagon);
          Triangle.IntersectWith(Hexagon);
          List&lt;long&gt; TriList2 = Triangle.ToList();
          temp = TriList2[2];     // excluding 1 and 40755
          return TriList.IndexOf(temp) + 1;
  
      }
  </code></pre>
  
  <p>The above code displays some very odd arithmetic behavior:</p>
  
  <ol>
  <li>Toggling the input <code>n</code> between 500,000 and 1,000,000 changes the 3rd term in the <code>Triangle</code> hashset after the intersection methods. Why does this occur is the sequences are monotonically increasing? </li>
  <li>Oddly the third term in <code>Triangle</code> is larger (value 4128501) when n = 1,000,000 and smaller when n = 500,000.</li>
  <li>Staying with n = 1,000,000, the resulting hashset <code>Triangle</code> includes 4128501 even though this value could not have possibly been in the <code>Pentagon</code> hashset.</li>
  <li><p>Trapping the value 4128501 at the <code>Pentagon.Add</code> method, we see it occurs at i = 56751. However, if the value to be added is i*(3*i-1)/2, it's plain to see that the resulting number should be greater than 4128501. Yet, the below is a direct output of the Immediate window:</p>
  
  <pre><code>?i
  567561
  ?i * (3*i-1)
  8257002
  ?i * (3*i-1)/2
  4128501
  </code></pre></li>
  <li><p>Lastly, when I use n = 1,000,000, and I try to call either <code>Pentagon.Max()</code>, <code>Triangle.Max()</code> or <code>Hexagon.Max()</code> I get the following message:</p>
  
  <pre><code> Hexagon.Max()  This expression causes side effects and will not be evaluate    long
  </code></pre></li>
  </ol>
  
  <p>Would appreciate any help on this very odd problem.</p>