Why does this program which finds the smallest triangle number with >500 factors crash? : <p>I have written the program below to solve Project Euler 12, which involves finding the smallest triangle number with over 500 factors.</p>

<p>I don't think there are major errors. I suspect memory optimization may be an issue. That being said, however, I need the unsigned long long int for the large triangle number that will eventually be the answer. I start my natural number sequence at triangleNumbers[0]=10,000,000,000. I know 9,000,000,000 has roughly 300 factors, so 10,000,000,000 was a "best guess."
That being said, however, I assume that 10,000,000,000 is the "first natural number" and continue adding subsequent natural numbers to get the "second" natural number and beyond (so triangleNumbers[1]=10,000,000,000 + 2, triangleNumbers[2]=10,000,000,000 +3, and so forth).</p>

<p>Any suggestions and tips would be appreciated. Thank you for helping a beginner improve.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;math.h&gt;

using namespace std;

bool keepRunning=true;

unsigned long long int naturalNumberCount=0;
unsigned long long int j=4;
unsigned long long int sum=0;

vector &lt;unsigned long long int&gt; triangleNumbers(0);

unsigned long long int totalFactors=0;
unsigned long long int trialDivisors=1;

unsigned long long int storer=0;

int main()
{
    triangleNumbers[0]=10000000000;
    triangleNumbers[1]=10000000002;
    triangleNumbers[2]=10000000005;
    triangleNumbers[3]=10000000009;
    triangleNumbers[4]=10000000014;
    //listed first few prime numbers above. j is set at 4 for this reason

    naturalNumberCount=5;
    //10000000014 is the 5th triangle number, and 5 is the 5th natural num
    //need this for recursive relation
    //5th triangle number = 4th triangle num + 5 (num + naturalNumberCount

    while(keepRunning)
    {
        for(trialDivisors;trialDivisors&lt;=(unsigned long long int)(sqrt(triangleNumbers[j]));trialDivisors++)
        {
            if(triangleNumbers[j]%trialDivisors==0)
            {
                totalFactors++;
                if(totalFactors&gt;499)//499 because the number itself will be a divisor of itself, so no need to check
                {
                    keepRunning=false;
                    break;
                }
                else
                {
                    keepRunning=true;
                }
            }
            else
            {
                keepRunning=true;
            }
        }
        //need the below to generate and store the next triangle number (as next element of array)

        naturalNumberCount++;//for recursive relation
        storer=triangleNumbers[j];//store the (j+1)'th triangle number, since we are changing j itself
        j++;//raise j, we gonna add the next value
        triangleNumbers[j]=(storer+naturalNumberCount);//the new value (last triangle number + current natural)
        totalFactors=0;//reset total factors to preclude any carry-over
    }


    cout&lt;&lt;triangleNumbers[j]&lt;&lt;flush;

    return 0;
}
</code></pre>
