Babylonian Territory

Down in Babylonian Territory, I successfully implemented the Babylonian Method to find the square root of a number. Again, I received help from Alex ( the same guy I talked about in my most recent post).

The structure of this program is very similar from the GCD because this one also has two classes: the main class and a ‘Number’ class. In the number class, again, there are two methods: one called ‘setValue’, that assign a value to the objects created in the main class; and another called ‘squareRoot’, in this one there are performed the mathematical calculations. In the main class, there are created two objetcs: val1 and val2. Then it is called the method squareRoot for val1, passing as parameter val2. In this method, there are four variables declared: a, b, div, and ave. Variable a is assigned the value of val1, and b is the parameter. Then there is a for iteration to perform the process of the Babylonian method three times. The variable div gets the value of a/b, and ave gets the value of the average of b and div. Finally b gets the value of ave to perform the next iteration.

I tried the code assigning a the value 7, and b the value 4, and the output I get was 2.6457672, a very accurate square root of number 7.

The reasoning for doing this code was based in this link.

Here I also leave the link and screens of my GitHub account where you can see my code. Greetings from the Babylonian Territory!




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

Logo de

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

Google photo

Estás comentando usando tu cuenta de Google. 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 )

Conectando a %s