JavaFX and Collatz Conjecture…

Following on from my previous posting about multi-threading I wanted to share with you a little “toy” I recently wrote. Upon understanding how to implement threading into JavaFX I wanted to write up an example to help me get a good understanding. I decided I would visualise Collatz Conjecture, feeling it was a perfectly simple principle that I could animate.

Firstly, for those of you who are not aware, Collatz Conjecture is nice and simple to perform. You take any natural number (N) and if it is odd then multiple it 3 and add 1, and if it is even divide by 2. This continues until you reach the number 1, below is a table of two examples for the natural numbers 4 and 5:

N = 4 N = 5
4 is even so 4/2. N = 2 5 is odd so 5*3 + 1. N = 16
2 is even so 2/2. N = 1 16 is even so 16/2. N = 8
8 is even so 8/2. N = 4
4 is even so 4/2. N = 2
2 is even so 2/1. N = 1

It isn’t really rocket science is it? It is quite interesting though. Try the number 27. There is more information on this available at, unsurprisingly, Wikipedia.

With that dealt with lets go back to the animation. I decided I wanted to animate this process, in fact I wanted each number to roll across the screen as it iterated through all of the numbers. As this idea progressed in my mind I happened across the thought that I could make it like a pack of cards being dealt. However the twist was the area of the screen number would land in would be determined by its value:

The final positioning (X,Y) of each rectangle, or card, is down to random positioning. This would give you a rather crude distribution.

Each card will rotate across the screen to its destination, one after the other (as if it had been dealt their). Hence I employed the threading logic as defined in my previous post, with each toss of the rectangle taking around 0.5 seconds to rotate and position itself across the screen.

All you, the user, need do is enter a number, watch the numbers being dealt across the screen and titter at the simplisity, oh and select again if you want to watch another number 🙂

Have a look, what do you think?

If anyone is interested I could go into more detail about the code?

JavaFX Applet

Advertisements

Tags: , , ,

One Response to “JavaFX and Collatz Conjecture…”

  1. limewire Says:

    lmao sweet story bro.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: