iterated square root
If you iteratively apply the square root, you will eventually get a number strictly less than two. Write a function that figures out how many times you have to apply the square root it results in a number less than two.
Examples
(iter-sqrt 1) ;=> 0 (since 1 is already less than 2)
(iter-sqrt 2) ;=> 1 (since the square root of 2 is less than 2)
(iter-sqrt 256) ;=> 4
Bonus: Write it in a way that uses stack frames and in a way that doesn't.
Thanks to this site for the challenge idea where it is considered Medium level in Python.
Email submissions to eric@purelyfunctional.tv before June 28, 2020. You can discuss the submissions in the comments below.
Since iter-sqrt must use some logic to test for a threshold, and several solutions here (including mine) use a smaller type threshold value to check the floating point value resulting from Math/sqrt, there is a risk of logic-spoling numeric type friction, e.g.:
Of course, since 2 and 2.0 are in different numeric types, they can't be compared reliably. I was using as a logic gate
(if (< n 2)...
in my own solution, but should have been(if (< n 2.0) ...)
.(In functions that accept any numeric type where the input is compared to a reference value, probably the best approach is to up cast (non-lossy cast) the input to that of the largest accepted type.)