Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project Euler Question 14 (Collatz Problem)

The following iterative sequence is defined for the set of positive integers:

n ->n/2 (n is even) n ->3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

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.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

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 :)

#include <stdio.h>
#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<LIMIT; i++)
 {
  printf("Current iteration : %d\n", i);
  iteration_count=count_iterations(i);
  if (iteration_count>max)
   {
   max=iteration_count;
   count=i;
   }

 }

 //iteration_count=count_iterations(113383); 
 printf("Count = %d\ni = %d\n",max,count);

}
like image 249
paradox Avatar asked Apr 15 '10 06:04

paradox


People also ask

What number has the longest collatz sequence?

My Algorithm The longest Collatz chain below five million contains 597 elements (and starts with 3732423). A brute-force algorithm solves this problem within a half a second. A smarter approach is to cache all chain lengths we encounter along the way.

What is Collatz conjecture problem?

The Collatz conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1.

Is Collatz conjecture hard?

It is considered difficult because no one has been able to solve it. The value of a problem like the Collatz conjecture isn't in the result. If the problem had been solved within a day of being proposed it might appear as an exercise somewhere.

Is there a pattern in the Collatz conjecture?

The Visual Pattern in the Collatz Conjecture and Proof of No Non-Trivial Cycles. We present the long sought visual pattern in the Collatz problem with the aid of a logarithmic spiral. Using this newly discovered pattern, we show that the Collatz problem is linked to primes via Jacobsthal numbers.


2 Answers

The reason you're stalling is because you pass through a number greater than 2^31-1 (aka INT_MAX); try using unsigned long long instead of int.

I recently blogged about this; note that in C the naive iterative method is more than fast enough. For dynamic languages you may need to optimize by memoizing in order to obey the one minute rule (but this is not the case here).


Oops I did it again (this time examining further possible optimizations using C++).

like image 66
Motti Avatar answered Sep 28 '22 06:09

Motti


Notice that your brute force solution often computes the same subproblems over and over again. For example, if you start with 10, you get 5 16 8 4 2 1; but if you start with 20, you get 20 10 5 16 8 4 2 1. If you cache the value at 10 once it's computed, and then won't have to compute it all over again.

(This is known as dynamic programming.)

like image 23
Jesse Beder Avatar answered Sep 28 '22 08:09

Jesse Beder