Below is a Python script that approximates the positive square root of positive integers. The algorithm makes guesses by continually averaging the upper and lower bound of a range of numbers that dynamically adjusts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Analyze the script and determine the error that exists in it from the choices below.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
In its current implementation, the script assumes that the positive square root of n is found within the range of 0 and n . However, this is not true when 0 < n < 1 because in this case the square root of n is actually greater than n . To correct this error, the initial setting of upperBound should be changed to this:
This change ensures that the initial range will be large enough to include the square root of n .