Chapter 4: Applied problem solving

Improving your square root estimator

face Josiah Wang

What you have just implemented is an example of an exhaustive search algorithm. It is also known as brute-force search, or generate and test. In summary, you go through all candidate solutions, and check whether it is correct.

Obviously, one weakness of an exhaustive search algorithm is that it can be slow, since you are going through so many possible candidate solutions.

One way to make exhaustive search algorithms faster is to reduce the search space. This means reducing the number of candidate solutions you need to search.

We have actually already done this previously - by limiting your search space to all positive floating point numbers up to a certain number of decimal places.

Now, can you think of any more clever ways to reduce your search space (other than reducing the number of decimal places)? You might be able to eliminate implausible candidates? Or perhaps arrange your candidates in a more clever way so that you are more likely to find your solution earlier?

Try to spend a short time thinking. If you have an idea, modify your program to see whether it works. If not, it is ok. This is just a quick exercise for you to try to make your program more efficient. We will discuss this in one of our lectures.

And if you already know of an existing algorithm for estimating roots called Newton’s method, hold on to that thought – I am planning to ask you to implement that in the next page!