# 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

- If a number is
**even**half it i.e, n/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); fullScreen(); // beginRecord(PDF, "collatz.pdf"); background(0); for (int i = 1; i < 100000; i++) { IntList sequence = new IntList(); int n = i; do { sequence.append(n); n = collatz(n); } while (n != 1); sequence.append(1); sequence.reverse(); float len = height/100.0; float angle = 0.15; resetMatrix(); translate(width/2, height); for (int j = 0; j < sequence.size(); j++) { int value = sequence.get(j); if (value % 2 == 0) { rotate(angle); strokeWeight(4); if(value%4==0) stroke(241,51,196,0); else if(value%3==0 && value%4==0) stroke(196,51,77,0); else stroke(128,37,77,0); } else { rotate(-angle); strokeWeight(2); 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.

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.

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’

https://codespeedy.com/adam-optimizer-in-deep-learning/

Nice to see how you literally copied all the content from The Coding Train video and published it as it was yours, not to mention that you didn’t even tried to make a version of your own nor give the credits where is due. This is stealing and you should feel ashamed for it!

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.

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

Just cuz we cheated before doesn’t mean that we think it’s right, nor does it mean that we will cheat again.