Rube Goldberg machine for checking if an integer is a power of 2

Alonso Del Arte
5 min readMay 17, 2024
Photo by Michał Parzuchowski on Unsplash

A software developer should never be proud of his program being longer than it needs to be. Unless of course the program is longer than it needs to be in a way that is as hilarious as a Rube Goldberg machine.

Sculptor Rube Goldberg is today famous mostly for his cartoons depicting very complicated machines that perform very simple tasks.

In this article, we’re going to make a Rube Goldberg function in Java for checking whether an integer is a power of 2 or not.

Legend has it that a king asked the inventor of chess to name his prize for creating such an entertaining game. The inventor asked for grains of rice on a chess board. That sounds like a very small reward for such a great game.

On the first square of the chess board, the king’s treasurer was asked to place one grain of rice. Okay, sure, whatever. On the next square, the treasurer would place two grains of rice. Then four grains on the next square, eight on the next, and so on and so forth.

This gives us the sequence 1, 2, 4, 8, 16, 32, 64, etc. On the 64th square, the treasurer would be expected to place 9,223,372,036,854,775,808 grains of rice.

After the treasurer places the very first grain of rice followed by two grains for the second square, there…

--

--

Alonso Del Arte

is a Java and Scala developer from Detroit, Michigan. AWS Cloud Practitioner Foundational certified