#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.

finalwsq07

And the github repository.

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s