Heron

Babylonian method

(also known as Hero’s method after the first century mathematician Hero of Alexandria)

How to calculate an approximate value of a square root with a numerical method. The process here is to find the square root of 2, a number that multiplied by itself gives 2. In a geometric sense it is the side length of a square with an area of 2. (This method works with any other positive number).

The Greek mathematician, engineer and surveyor Hero of Alexandria took a rectangle with area 2 and transformed it step by step so that it resembled a square more and more. The width of this shape closely resembling a square is the side length you are looking for.

Method - the algorithm

First, in a coordinate system draw the given rectangle into the coordinate system. It is the beginning of the approximation. The initial step (0) so to speak. The bottom left corner of the rectangle should lie in the origin (0,0) of the coordinate system

step $${x}$$ $${y}$$
0 $${x_0 = 2}$$ $${y_0 = 1}$$
1 $${x_1 = }$$ $${y_1 = }$$
2 $${x_2 = }$$ $${y_2 = }$$
3 $${x_3 = }$$ $${y_3 = }$$
4 $${x_4 = }$$ $${y_4 = }$$

step 1

The shape is a rectangle and not a square. So, now you have to find a width which is more suitable. You do this by taking the mean of the width and the height to calculate a new $${x_1 = \frac{x_0 + y_0}{2}}$$

step 2

For the rectangle still to have the area of 2 (A = 2), you need to adjust the height accordingly. Using $${A = x \cdot y}$$ to derive the formula to calculate the new height $${y_1 = \frac{A}{x_1}}$$

repeat steps 1 and 2 to find the next approximations.

Depending on the magnitude (size) of the number, the number of iterations you need for a number of decimal places of accuracy varies.

example

user@linux-computer:~/$ python heron.py -i 12

 __   __ ____ ____ ____ __   _
|  | |  |  __|    \    \  \ | |
|  |_|  | |__   D  |    |  \| | 
|   _   |  __|    /  O  |     |
|  | |  | |__  |\ \     ||\   |
|__| |__|____|_| \_\___/_| \__| 

Babylonian algorithm for finding square roots (c) sdw

x_ 0 =  12.0000000000   ---  y_ 0 =  1.0000000000
x_ 1 =  6.5000000000   ---  y_ 1 =  1.8461538462
x_ 2 =  4.1730769231   ---  y_ 2 =  2.8755760369
x_ 3 =  3.5243264800   ---  y_ 3 =  3.4049058929
x_ 4 =  3.4646161864   ---  y_ 4 =  3.4635871203
x_ 5 =  3.4641016534   ---  y_ 5 =  3.4641015769
x_ 6 =  3.4641016151   ---  y_ 6 =  3.4641016151
x_ 7 =  3.4641016151   ---  y_ 7 =  3.4641016151
  
The square root of  12.0  is approximately  3.4641016151

using the following python script heron.py

# /bin/python --------------
# heron algorithm 
# author: sebastian williams
#---------------------------

import math
import re
import os
import sys
import string
from Tkinter import *
from optparse import OptionParser

def logo():
    print('')
    print(' __   __ ____ ____ ____ __   _')
    print('|  | |  |  __|    \    \  \ | |')
    print('|  |_|  | |__   D  |    |  \| | ')
    print('|   _   |  __|    /  O  |     |')
    print('|  | |  | |__  |\ \     ||\   |')
    print('|__| |__|____|_| \_\___/_| \__| ')
    print('')
    print('Babylonian algorithm for finding square roots (c) sdw')
    print('')
	
def calc_root(x_value,n_iter,graph_out):
	new_x = x_value
	new_y = 1
	g = graph_out
	i = 0
	print "x_",i,"= ",("{0:.10f}".format(new_x)),"  ---  y_",i,"= ",("{0:.10f}".format(new_y))
	if (g == 1):
		print "+",int(new_x)*"--","+"
		k = 0
		while (k < new_y):
			print "|", int(new_x)*"  ","|"
			k = k + 1
		print "+",int(new_x)*"--","+"
	while (i < n_iter):
		new_x = (new_x + new_y)/2
		new_y = x_value/new_x
		print "x_",i+1,"= ",("{0:.10f}".format(new_x)),"  ---  y_",i+1,"= ",("{0:.10f}".format(new_y))
		if (g == 1):
			j = 0
			print "+",int(new_x)*"--","+"
			while (j < new_y):
				print "|", int(new_x)*"  ","|"
				j = j +1
			print "+",int(new_x)*"--","+"
		i = i + 1
	print ('  ')
	print 'The square root of ', x_value, ' is approximately ', ("{0:.10f}".format(new_x))
	print ('  ')

def main():
    parser = OptionParser(usage='"%prog -i number"')
    parser.add_option("-i", dest="sqroot", help="enter the number you want to calculate the square root for")
    parser.add_option("-n", dest="iter", help="enter the number of iterations")
    parser.add_option("-g", dest="graph", help="enter 1 to display squares")
    (options, args) = parser.parse_args()
    if not options.sqroot:
        parser.error('no root given, use -i [square root]')
    sq_value = float(options.sqroot)
    if options.iter:
    	num_iter = float(options.iter)
    	if options.graph:
    		graph = int(options.graph)
    	else:
    		graph = 0
    	calc_root(sq_value,num_iter,graph)
    else:
    	num_iter = 7
    	if options.graph:
    		graph = int(options.graph)
    	else:
    		graph = 0
    	calc_root(sq_value,num_iter,graph)
    
if __name__ == "__main__":
    main()

(c) 2025 sebastian.williams[at]sebinberlin.de - impressum und datenschutz - Powered by MathJax & XMin & HUGO & jsxgraph & mypaint