#WSQ07-Babylonian Square Root

First of all, the Babylonian square root is a Babylonian algorithm for calculating the Babylonian Square Root (duh…) of a given number, say n, but in a non conventional way. This algorithm consists of making a guess (i.e.: think of an approximate value to the actual square root), say g, then, you add g, to the ratio of the number and the guess, and divide that by 2.
So, this is:

Babylonian square root equation

And the result of this equation is the new guess.
We do this process several times and then the final guess is the result of the square root.

This is the class, without the method for calculating the square root.

Screenshot from 2016-02-04 12:14:39

So the algorithm says that we need to pick a near number to the actual square root. So I imported the Random class to pick some random number near the half of the number, because YOLO. Then I used that number as the first guess and executed the algorithm.

And here’s the final code.


And the github repository.

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