Square Root Babylonian Style

Introduction

Ancient trick to calculate ANY square root (YouTube)

The video's algorithm require an initial seed value (initial estimate of the square root). Given a positive real number S, use the "nearest perfect square" to S as the seed.

Note: The seed must be a non-zero positive number and should be between 1 and S. If the seed is far away from the square root being calculated, the algorithm will require more iterations and thus will be less efficient.

Project #1

Watch the video. Create an interactive program to

  1. ask the user for a positive real number greater than zero
  2. calculate its square root using the algorithm described in the video
    (calculate to a least 3 or 4 decimal places)
  3. loop

Note: For testing verify the answer using math.sqrt().

Project #2

Create a table of square roots and perfect squares. (Integer square root 1 to 20.)

Square RootPerfect Square
11
24
39
416
525
......

Links

Methods of computing square roots (Wikipedia)

A Proof That The Square Root of Two Is Irrational (YouTube)

FYI

nApproximately nApproximately
21.41421 103.16228
31.73205 π1.77245
52.23607 e1.64872
62.44949 Your favorite number

Useful Code?

import math # ---------------------------------------------------------- # ---- Find the nearest perfect square to a given number # ---- ----------------------------------------------------- # ---- Using the nearest perfect square makes the video's # ---- algorithm more efficient. See the Wikipedia article. # ---- ----------------------------------------------------- # ---- Apparently the Babylonians used a table to do this. # ---- A table is simple, fast, and only needs to be # ---- created/calculated one time. # ---- ----------------------------------------------------- # ---- Note: this algorithm was found on the web # ---- 1. calculate the square root of the number # ---- 2. round it to the nearest integer # ---- 3. square the rounded integer to get the closest # ---- perfect square # ---- 4. return the nearest perfect square root, and # ---- the perfect square # ---------------------------------------------------------- def nearest_perfect_square(num): num_sqrt = math.sqrt(num) round_sqrt = round(num_sqrt) return (round_sqrt,round_sqrt*round_sqrt)

import user_interface as ui # ---------------------------------------------------------- # ---- test if a string is a number greater than zero # ---- if true, return the number # ---------------------------------------------------------- def positive_number_greater_than_zero(string): tf,n = ui.is_integer(string) if tf: if n > 0: return (True,n) return (False,0) tf,n = ui.is_float(string) if tf: if n > 0.0: return (True,n) return (False,0.0)

import user_interface as ui # ---------------------------------------------------------- # ---- main - get user input loop # ---------------------------------------------------------- while True: print() s = ui.get_user_input( 'Enter a positive number greater that 0: ') if not s: break # empty string? tf,num = positive_number_greater_than_zero(s) if not tf: print() print('Bad input. Try again.') continue nps_root,nps = nearest_perfect_square(num)

Note: Calling the algorithm "Babylonian Style" is an homage to In-N-Out Burger's secret menu.