'Integer Square root of a number
It's not that I don't understand how to find the integer square root of a number. I know several ways of finding them using Python and C++.
It's just that this algorithm is really messing with my brain. And having to write it in SML is another headache.
Please help me in understanding this algorithm. Do note that this should be using recursion:
The integer square root of 𝑛 is the integer 𝑘 such that 𝑘²≤𝑛<(𝑘+1)². The integer square root can be computed using the following inductive process:
- Compute the integer square root 𝑖 of 𝑚 = 𝑛 div 4 recursively. We then have that 𝑖²≤𝑚<(𝑖+1)².
- Since 𝑚 and 𝑖 are integers, we have that (𝑚+1)≤(𝑖+1)². We thus have (2𝑖)²≤4𝑚≤𝑛<4𝑚+4≤(2𝑖+2)².
- Hence we have that the integer square root of 𝑛 is either 2𝑖 or 2𝑖+1.
Write a recursive ML program corresponding to the above algorithm.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
