I think have worked out a solution for finding the largest Collatz Sequence below a LIMIT but it takes a lot of time! My question really is where the source for this would be: code, software, hardware?
I have done some research on the Internet and have seen people doing it pretty much the same way with runtimes around 2000ms. Whereas my computer does not even come that far! I'm using Visual Studio, C++. Btw, after debugging it can be seen that the calculation suddenly (stops, struggles, hangsup?) when determining the sequence of 113383.
Here is my little code snippet:
(The parts with map and the first if are possible additions I thought could speed up the whole process but don't. If I leave them out its the same speed-wise)
#include <iostream>
#include <set>
#include <map>
using namespace std;
#define LIMIT 1234567
int main()
{
int n = 0;
int compare = 0;
int longest = 0;
int counter = 1;
set <int> nums2excl;
map <int, int> mappi;
map <int, int>::const_iterator itMap = mappi.begin();
for (int i = 13; i < LIMIT; i++)
{
if (nums2excl.find(i) != nums2excl.end())
{
continue;
}
n = i;
while (n != 1)
{
itMap = mappi.find(n);
if (itMap != mappi.end())
{
counter += ((*itMap).second - 1);
break;
}
if (n % 2 == 0)
{
n /= 2;
}
else
{
n = 3 * n + 1;
if (n > i)
{
nums2excl.emplace(n);
}
}
counter++;
}
mappi.emplace(i, counter);
if (counter > compare)
{
compare = counter;
longest = i;
//Test
cout << i << endl;
}
counter = 1;
}
return 0;
}
Now, am I missing some mistakes in the code, mistakes regarding efficiency or is my laptop just too slow for this?