title: Project Euler Question 14 (Collatz Problem)
body: <p>The following iterative sequence is defined for the set of positive integers:</p>
  
  <p>n ->n/2 (n is even)
  n ->3n + 1 (n is odd)</p>
  
  <p>Using the rule above and starting with 13, we generate the following sequence:</p>
  
  <p>13  40  20  10  5  16  8  4  2  1
  It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.</p>
  
  <p>Which starting number, under one million, produces the longest chain?</p>
  
  <p>NOTE: Once the chain starts the terms are allowed to go above one million.</p>
  
  <p>I tried coding a solution to this in C using the bruteforce method. However, it seems that my program stalls when trying to calculate 113383. Please advise :)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #define LIMIT 1000000
  
  int iteration(int value)
  {
   if(value%2==0)
    return (value/2);
   else
    return (3*value+1);
  }
  
  int count_iterations(int value)
  {
   int count=1;
   //printf("%d\n", value);
   while(value!=1)
   {
    value=iteration(value);
    //printf("%d\n", value);
    count++;
   }
   return count;
  }
  
  int main()
  {
   int iteration_count=0, max=0;
   int i,count;
  
  
   for (i=1; i&lt;LIMIT; i++)
   {
    printf("Current iteration : %d\n", i);
    iteration_count=count_iterations(i);
    if (iteration_count&gt;max)
     {
     max=iteration_count;
     count=i;
     }
  
   }
  
   //iteration_count=count_iterations(113383); 
   printf("Count = %d\ni = %d\n",max,count);
  
  }
  </code></pre>