100-Level Challenge: Square Root
You've finished the 100-level and used the instructions we've learned to implement Euclid's algorithm.
Here's another algorithm.
This time, the implementation is up to you.
The Babylonian method
The Babylonian method is an old algorithm for approximating square roots.
Did Babylonians actually do this?
Yes — methods equivalent to this one appear in ancient Babylonian mathematics.
Babylonian mathematicians used repeated approximations to calculate square roots thousands of years before electronic computers existed.
The method is still closely related to techniques used in numerical computation today.
We'll use an integer version of it. Since NCL integer division discards the remainder, our program will calculate the integer square root.
For example:
sqrt(25) = 5
sqrt(20) = 4
sqrt(10) = 3
The procedure works like this:
- Take the absolute value of the input.
- If the input is less than
2, the input itself is the result. - Make an initial guess by dividing the input by
2. - Calculate a new guess:
new guess = (input / guess + guess) / 2
- If the new guess is greater than or equal to the previous guess, the previous guess is the result.
- Otherwise, use the new guess and repeat from step 4.
An example
Let's find the integer square root of 25.
Start with:
input = 25
guess = 25 / 2
guess = 12
Calculate a new guess:
new = (25 / 12 + 12) / 2
= (2 + 12) / 2
= 7
7 is less than 12, so continue with 7 as the guess:
new = (25 / 7 + 7) / 2
= (3 + 7) / 2
= 5
5 is less than 7, so continue again:
new = (25 / 5 + 5) / 2
= (5 + 5) / 2
= 5
The new guess is no longer less than the previous guess, so the result is:
5
Your program
Write an NCL program that follows the procedure above and displays the integer square root.
Define the input as a constant so that it can easily be changed:
#input 25
Your program should handle negative inputs by taking their absolute value before beginning the calculation.
Try it with:
| Input | Result |
|---|---|
-25 |
5 |
0 |
0 |
1 |
1 |
2 |
1 |
3 |
1 |
4 |
2 |
10 |
3 |
25 |
5 |
99 |
9 |
100 |
10 |
If your program produces all of those results, try some values of your own.
Stuck?
You already have every NCL instruction you need.
The hints below become progressively more specific. Start at the top and only open as many as you need.
What needs a memory location?
The input never changes, so it doesn't need a memory location of its own.
The values that change are the current guess and the next guess. Give each one a memory location.
Still stuck?
Here's one way to set them up:
#input 25
#guess $000
#next $001
How do I handle the absolute value?
The input is a constant, so you can't replace it with its absolute value.
You do need the absolute value somewhere, though.
One of the memory locations you've already allocated can hold it.
Still stuck?
ABS can read from one value and write to a memory location:
ABS #guess #input
Now #guess contains the absolute value of the input.
What about 0 and 1?
After the previous step, #guess already contains the absolute value of the input.
If that value is 0 or 1, it is already the answer.
You can skip the rest of the calculation entirely.
Still stuck?
Both cases can be handled with one branch:
BLT :done #guess 2
How do I make the first guess?
For every remaining input, the initial guess is the input divided by 2.
You need to keep the absolute input in #guess for the moment, so put the initial guess somewhere else.
Still stuck?
Store it in #next:
DIV #next #guess 2
What happens at the start of each iteration?
At this point, #next contains the estimate you want to use for the next calculation.
Before calculating another estimate, that value needs to become the current guess.
Still stuck?
The beginning of the loop can look like this:
:loop
MOVE #guess #next
How do I calculate the next guess?
The calculation:
new guess = (input / guess + guess) / 2
can be performed one operation at a time.
You don't need another memory location for the intermediate results. #next can be overwritten as the calculation progresses.
Still stuck?
Translate the expression into three instructions:
DIV #next #input #guess
ADD #next #next #guess
DIV #next #next 2
How do I repeat the calculation?
The algorithm repeats while the new guess is smaller than the current guess:
next < guess
If that condition is true, execution needs to return to :loop.
If it isn't, the calculation is finished.
Still stuck?
That's one branch:
BLT :loop #next #guess
How do I display the result?
When the loop finishes, think about which value contains the result.
The last value in #next was rejected because it was no longer smaller than the current guess.
Still stuck?
Display #guess:
:done
D.TXT #guess
D.BLT
Show me one solution
There are several ways to arrange the program. Here's one:
#input 25
#guess $000
#next $001
ABS #guess #input
BLT :done #guess 2
DIV #next #guess 2
:loop
MOVE #guess #next
DIV #next #input #guess
ADD #next #next #guess
DIV #next #next 2
BLT :loop #next #guess
:done
D.TXT #guess
D.BLT
Trace the solution
Change the input to 15:
#input 15
The program progresses through these states:
| Step | #guess |
#next |
Result |
|---|---|---|---|
After ABS |
15 | — | Continue |
| Initial guess | 15 | 7 | Enter loop |
| Iteration 1 | 7 | 4 | 4 < 7 — loop |
| Iteration 2 | 4 | 3 | 3 < 4 — loop |
| Iteration 3 | 3 | 4 | 4 < 3 is false — finish |
Execution falls through to :done with #guess containing 3.
The program displays:
3
When you're ready to continue, NCL 201: Remembering Text introduces strings and string memory.