A Mini-Assignment in JavaScript

When you want to find the square root of a number on the calculator, you enter that number into the calculator and press the Ö button. However, have you ever wondered how the calculator does it? It uses an iterative formula:

An iterative formula starts with an estimate for the square root and then applies a mathematical equation to get a better estimate. Then it applies the same equation on that new estimate to get an even better one. Here is the formula:

New estimate =

number - 1

+ 1
old estimate + 1

The numerator (top number) of the fraction is always one less than the number whose square root we are trying to find, so if we want the square root of 7, then the top number of the fraction (the part marked number - 1) should be 6.

Suppose, for example, we want the square root of 2. The top of the fraction should be 1 in that case (= 2 - 1). Let's suppose we start with an initial estimate of 1.5 (a fairly good estimate of the square root of 2). We can get a better estimate using this formula:

1

+ 1 = 1.4
1.5 + 1

This is a slightly closer estimate. Then we put that in the formula and get an even better estimate:

1

+ 1 = 1.4167
1.4 + 1

In fact, if we carry on doing this many times, we gradually narrow in on the correct answer, which is 1.41421356. It only takes about 20 steps for us to achieve 6 or 7 decimal places of accuracy.

And now for the assignment itself

  1. Create a text file called sroot.htm containing the <SCRIPT> and </SCRIPT> tags.

  2. Declare a variable called x inside the program. This will hold the number whose square root is to be found. Use the prompt() function to get the value of this variable from the user, but remember, prompt() returns a string, not a number. You will have to put in a step that converts the string into a number which is stored in x.

  3. Declare a variable called old_estimate which contains the first estimate of the root. I suggest that your first estimate should be half of the original number, so if you wanted the square root of 13, your first estimate would be 13 / 2 = 6.5.

  4. Declare a new variable called new_estimate, and, in a separate statement assign it using the formula above. Why is it important that the var statement declaring the variable and the assignment using the formula are done in different statements?

  5. Put a statement after the formula statement that copies the value of the new estimate into the variable old_estimate (i.e. old_estimate becomes a copy of new_estimate). This will be our way of telling the program to "recycle" the value of new_estimate.

  6. Enclose the previous two statements (i.e. the formula assigning new_estimate and the copying of new_estimate into old_estimate) in a for loop. This loop should run for an arbitrary number of steps: about 50 steps should be enough to give good accuracy in the value produced.

  7. Finally, after the loop has terminated, using either document.write() or alert(), which displays the final value of the root produced and an appropriate message.

Now you should test the program by running it and entering various values to see if the program produces the same output as a calculator. Bear in mind the following points:

Extending the Assignment

The calculator uses a similar formula when calculating high roots than square roots. For instance, when calculating cubed roots of numbers (where the cubed root of 8 is 2), the formula becomes the following:

new estimate =

number - 1

+ 1
(old estimate)2 + (old estimate) + 1

and when calculating fourth roots, the formula becomes more complex again:

new estimate =

number - 1

+ 1
(old estimate)3 + (old estimate)2 + (old estimate) + 1

By now you will have spotted a pattern., and this pattern continues up for as high a root value as you need. You can easily adapt your program so that it asks the user not only for the number to be "rooted" but also for the value of the root (2 for square root, 3 for cubed root etc.) In the next few lines, I have given a "pseudocode" version of how the denominator (bottom number) of the fraction could be constructed easily based on this root number:

var denom;
denom = 1;
for loop counts from 1 to (root_number - 1)
  {  multiply denom by the old_estimate and then add 1
  }

The loop should be carried out once for square roots (i.e. 2 - 1 times), twice for cubed roots (i.e. 3 - 1), three times for fourth roots etc. You can extend the assignment so that your program can calculate roots of any power.

A word of warning!

Does the formula for higher roots work? Try calculating the square root of 64 (which should be 8). Did that work? What about the cubed root of 64 (which should be 4)? Try calculating the cubed roots of numbers less than 20 and comparing them with values produced by the calculator. Now try the same with numbers greater than 20.


Go back