Programming Interview Questions 27: Squareroot of a Number | Arden DertatArden Dertat
Find the squareroot of a given number rounded down to the nearest integer, without using the sqrt function. For example, squareroot of a number between [9, 15] should return 3, and [16, 24] should be 4.
The squareroot of a (non-negative) number N always lies between 0 and N/2. The straightforward way to solve this problem would be to check every number k between 0 and N/2, until the square of k becomes greater than or rqual to N. If k^2 becomes equal to N, then we return k. Otherwise, we return k-1 because we're rounding down. Here's the code:
Read full article from Programming Interview Questions 27: Squareroot of a Number | Arden DertatArden Dertat
No comments:
Post a Comment