0% found this document useful (0 votes)
51 views3 pages

Handouts Lectureslides Lecture3 5 PDF

This document discusses approximate solutions for finding square roots. It proposes starting with an exhaustive enumeration approach that takes small steps to generate guesses until finding one close enough to the true square root. An example code is provided that iteratively increases its guess by a step size until the guess squared is within a set epsilon of the input number. Some observations note that the step size is a tradeoff between speed and accuracy, and that a more efficient approach than exhaustive enumeration would be needed.

Uploaded by

priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Handouts Lectureslides Lecture3 5 PDF

This document discusses approximate solutions for finding square roots. It proposes starting with an exhaustive enumeration approach that takes small steps to generate guesses until finding one close enough to the true square root. An example code is provided that iteratively increases its guess by a step size until the guess squared is within a set epsilon of the input number. Some observations note that the step size is a tradeoff between speed and accuracy, and that a more efficient approach than exhaustive enumeration would be needed.

Uploaded by

priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Approximate

solu/ons
Suppose we now want to nd the square root
of any non-nega/ve number?
Cant guarantee exact answer, but just look
for something close enough
Start with exhaus/ve enumera/on
Take small steps to generate guesses in order
Check to see if close enough

Example code
x = 25!
epsilon = 0.01!
step = epsilon**2!
numGuesses = 0!
ans = 0.0!
while (abs(ans**2 - x)) >= epsilon and ans <= x:!
ans += step!
numGuesses += 1!
print('numGuesses = ' + str(numGuesses))!
if abs(ans**2-x) >= epsilon:!
print('Failed on square root of ' + str(x))!
else:!
print(str(ans) + ' is close to the square root
of ' + str(x))!

Some observa/ons
Step could be any small number
If too small, takes a long /me to nd square root
If make too large, might skip over answer without
geGng close enough

In general, will take x/step /mes through code


to nd solu/on
Need a more ecient way to do this

You might also like