Collatz Conjecture

Collatz Conjecture

This post was originally published at https://chrisrng.svbtle.com/collatz-conjecture.

In this blog post we deviate a little from the ES6 parade I’ve been writing here. We dive into the Collatz Conjecture (otherwise known as the 3n + 1 conjecture) on today’s blog. The Collatz conjecture is the simplest open problem in mathematics.

The conjecture states that:

Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called “Half Or Triple Plus One”, or  HOTPO) indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.

Basically if we give the number 10 the breakdown is as follows: 10 > 5 > 16 > 8 > 4 > 2 > 1 (match!). The conjecture states that every arbitrary positive integer will reach back to the number 1 after jumping through the hoops.

Source: https://xkcd.com/710/

Trivial Code

So to automate most of the rudimentary math involved we will write a program to run through the possibilities until we reach the number 1 or I guess run in perpetuity if in fact the conjecture is false.

function collatz(num = null, trace = []) {
  // Type checking and check if positive int
  // Zero is defined as neither negative nor positive
  if (typeof num !== 'number' || num <= 0) {
    return new Error(`Invalid argument: ${num}`);
  }

  // Log out the trace
  trace.push(num);

  // Check to see if its a match
  if (num === 1) {
    console.log('It matches!');
    return trace;
  }

  // Main recursion
  if (num % 2 === 0) {
    // even
    return collatz((num / 2), trace);
  } else {
    // odd
    return collatz(((num * 3) + 1), trace);
  }
} 

Enhancements to consider

  • Dynamic programming pattern like Memoize would provide some benefits by saving a cache of the numbers already calculated so there is no need to recompute
  • We could set an arbitrary limit on how deep the stack trace goes before we give up and analyze if the given num is in fact non-compliant with the Collatz conjecture
  • Tail recursion (implemented) is only active in strict mode es2015 not es5

Importance in Computer Science

Well… we could use the Collatz conjecture as an interview question but going beyond the most basic use case of the mathematical function f(x)required by the conjecture we see that the Collatz conjecture has impacts on Number Theory and Computer Science.

Gerhard Opfer, in 2011, published a paper that claims to have prove the Collatz conjecture. This proof, however, was later proven to have a flaw and was debunked.

The Collatz conjecture is similar to that of Fermat’s Last Theorem which is decidedly simple to understand but unbelievably hard to prove.

The same can be said about analyzing code.

Some minuscule codebase can have a very high complexity while other very large (cough “enterprise”) projects are very easy to implement and comprehend.

Think about validating an email address using regex.

A solution to Collatz conjecture would have consequences for symbolic dynamics, discrete mathematics, decidability, and number theory. Basically the back bone of computer science would be changed.


To view or add a comment, sign in

More articles by Chris Ng

  • My hopes for Ember in 2019 – Better JavaScript Community Visibility

    This blog is my input on what I believe the priorities should be for Ember in 2019, in response to the second annual…

    3 Comments
  • ForwardJS Ottawa 2019 Recap

    ForwardJS Ottawa is a 2 day conference held at the Adobe offices in Ottawa, Ontario, Canada. The conference had…

    2 Comments
  • The Open Office Layout

    Let's talk about a contentious topic among developers – the open office layout. While some developers love it, the…

    4 Comments
  • WYSIWYG 3: Browser Peace?

    Hey all, In this week’s issue of WYSIWYG, we’ll be taking a break from the security issues and jumping into browser…

    1 Comment
  • WYSIWYG 2: How do we protect our data?

    Hey all, Fresh off the security breaches from last week’s edition, we have even more data compromises to report. This…

    1 Comment
  • WYSIWYG 1: Security Issues All Around

    Hi there! Welcome to the first edition of WYSIWYG by Chris Ng! WYSIWYG, pronounced "wiz-ee-wig", is an acronym for…

  • Make It Beginner Friendly – My hopes for Ember in 2018

    This blog is in response to Katie Gengler’s Call for Blog Posts with the goal of having shared, clear, and published…

    13 Comments
  • EmberConf 2018 Recap

    EmberConf is the best place to meet the people behind the Javascript framework Ember.js.

  • How to Write About What You're Working On

    Note: While I write this from a software engineer’s point of view, many parts of this article are easily transferable…

    3 Comments
  • Dealing with the Communication Paradox

    TL;DR: Getting to the point with as much detail as needed reduces both problems of lacking in context and information…

Others also viewed

Explore content categories