thanks in advance for taking a look at this.
I'm getting a Windows error message when I try to run this program I wrote in C in Code::Blocks. The funny thing is it compiles fine, and if I lower the upper bound of what I'm testing the program runs fine as well.
Details:
When I try to run the program, first I get a Windows popup that says "X.exe has stopped working. Windows is checking for a solution to the problem". Shortly this changes to "X.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available. (Close program)" I click the close program button, and then I see the command prompt that says "Process returned 255 <0xFF> execution time 3.940 s Press any key to continue".
I have Windows 8.
I'm using the GNU GCC compiler.
If I change "upto" to 100000, the program works fine.
Here's the code:
/************************************************
* Finds the starting integer under 1000000 that
* produces the longest Collatz sequence, and the
* length of said sequence.
*************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#define upto 1000000
int main()
{
long i;
long long j;
long long max = LONG_LONG_MAX;
long length = 0;
long number = 0;
long penull = 0;
long len[upto];
for (i = 0; i < upto; i++) (len[i] = 0);
// counts length of Collatz sequence for starting integers from 1 to 999999
for (i = 1; i < upto; i++)
{
j = i;
while (j != 1)
{
assert (j <= (max - 1)/3);
if (j%2 == 0) (j = j/2);
else (j = 3*j + 1);
len[i]++;
if (j < i)
{
len[i] = len[i] + len[j];
j = 1;
}
}
// stores length of the longest sequence and the starting integer producing it
if (len[i] > length)
{
length = len[i];
number = i;
}
// stores a duplicate length for later comparison
else if (len[i] == length) (penull = len[i]);
}
if (length == penull) (printf("There are at least two!"));
else printf("%ld produces a Collatz sequence of length %ld", number, length + 1);
return 0;
}