Chapter 4: Applied problem solving

Square root estimator (Step 1)

face Josiah Wang

Now, let us try to design a program that reads in a floating point number n from the user, and estimates the square root of n.

Of course, you could have just used math.sqrt() for this. Or even simply n**0.5. But let’s say such a function or feature does not exist, and you will have to develop this from scratch.

Obviously, you will not have to implement such a thing in real life. The aim of this exercise is to try to get you to practise how to develop algorithms (and implementing the algorithm in Python).

This problem is actually more challenging than what you have previously done, so I will guide you to think through this one.

Now, remember the mantra - do not jump straight into coding!

Forget about Python for a bit, and let us just focus on problem solving for now.

Step 1: Understand and formulate the problem

The first step is an easy step, since the problem is well-defined. But it will be a good exercise to confirm your understanding.

Understanding the problem

What is the input?

What is the output?

And what are you trying to do with the input?

The answer should be clear.

The input should be a floating point number. But what kind of floating point number? Can it be a negative number? Can it be zero? Try to define any assumptions clearly before you start.

The output is also a floating point number (which represents an estimate to the square root of the input).

Your program should then take the input number and estimate the square root, and output the estimated square root.

Any external resources needed? I cannot think of anything. The problem is simple enough!