Collatz Conjecture in Java with Processing

Collatz Conjecture in Java with Processing

Hi coders, today we will discuss Collatz conjecture in Java.

Imagine something simple yet complicated to prove that it’s simple. Today we’re talking about such a fuzzy problem which breaks your mind finding the proof – The Collatz Conjecture. Collatz was first to identify such a fatigue-causing problem. Which simply stated that

  1. If a number is even half it i.e, n/2
  2. If a number is odd – 3*n+1, well there’s literally no dichotomy to name the function.

For instance, any number like 10, on Collatz condition would go something like dozens of steps and stop at 1 and after 1, the only sequence we get is 1 4 2 1 4 2 so on.

Numberphile already posted a video on this on youtube.

Why is Collatz conjecture important?

A forum on the stack exchange discusses this is important in decidability. Now, let’s not dive deep into mathematics and I just say there might be a different way to solve this problem which is undecidable.

Program: Collatz Conjecture in Java with Processing

Now, using Java let’s generate a tree for Collatz conjecture. I used the Processing environment with java mode.

import processing.pdf.*;

void setup() {
  //size(1200, 600);
  // beginRecord(PDF, "collatz.pdf");
  for (int i = 1; i < 100000; i++) {
    IntList sequence = new IntList();
    int n = i;
    do {
      n = collatz(n);
    } while (n != 1);
    float len = height/100.0;
    float angle = 0.15;
    translate(width/2, height);
    for (int j = 0; j < sequence.size(); j++) {
      int value = sequence.get(j);
      if (value % 2 == 0) {
        else if(value%3==0 && value%4==0)
      } else {
        if (value%5==0)
        stroke(255, 2);
        else stroke(196,2);
      line(0, 0, 0, -len);
      translate(0, -len);
    // Visualize the list
  // endRecord();
int collatz(int n) {
  // even
  if (n % 2 == 0) {
    return n / 2;
    // odd
  } else {
    return (n * 3 + 1)/2;

We’ve generated trees for the first 10k numbers here in the above program. The big ugly display picture on the top is not clickbait, it’s the program output ;P.

If you’re a data science enthusiast check out my profile for various content like Adam Optimizer.

I hope you like it.

4 responses to “Collatz Conjecture in Java with Processing”

  1. Hemanth says:

    Thank you for copy-pasting the code from The Coding Train and changing a few variables here and there and presenting it as yours. Kudos, Sai Ram! The world needs more people like you! If you could care less of mentioning at the end of the article where you’ve copied the code from, folks floating around here would know that its Daniel’s piece of code and not yours.

  2. Sai Ram says:

    Really, I’m so flabbergasted with the amount of English youse spoke and thank you for such kind words.
    Again, thanks it’s the first comment I received on my blog posts, please do check other content and comment on them too. I really look forward.
    Here, is a link to a post, I really hope you take the time around and enjoy it.
    Happy ‘Neural Network November’

  3. Hshsbduduejruc says:

    You CHEATER! You could have at least mentioned Daniel in this website! You are just like other liars. Stealing codes from others and saying that I did it.

  4. dexterroll says:

    Well thanks, everyone cheats, haven’t you cheated or lied in life. Gods must be crazy.

Leave a Reply

Your email address will not be published. Required fields are marked *