Saturday, December 20, 2014

Collatz Conjecture

The Collatz Conjecture, known by many other names, including 3n+1, and the Ulam Conjecture, is a problem, that while stated simply, has proven extremely difficult to solve.

The problem simply stated is, for any number n, if odd, multiple by three and add one, or, if even, divide by two, will the sequence converge on one?

Example: if you start with n = 10, then:

  • 10 % 2 = 0
  • n = 10 / 2 = 5
  • 5 % 2 = 1 
  • n = 3 * 5 + 1 = 16
  • 16 % 2 = 0
  • n = 16 / 2 = 8
  • 8 % 2 = 0
  • n = 8 / 2 = 4
  • 4 % 2 = 0
  • n = 4 / 2 = 2
  • 2 % 2 = 0
  • n = 2 / 2 = 1
  • n = 1

Richard Guy lists it as one of the problems you should not try to solve, or at the very least, without first referencing the work that has been done to date. Namely, Eric Roosendaal's site appears to be the most current resource.

Below is sample code to print the sequence for any starting value of n:

def collatz(x):
...     while(x > 1):
...             if((x % 2) == 0):
...                     x = x / 2
...             else:
...                     x = (3 * x) + 1

...             print x

No comments: