Problem
Implement a fast integer square root function that takes in a 32-bit unsigned integer and returns another 32-bit unsigned integer that is the floor of the square root of the input.
Solution
using System;
namespace JC
{
class BinarySearch
{
static int SearchSqrt(int value)
{
int low = 0;
int up = 65535;
if (value < 0)
{
throw new ArgumentException("Input value cannot be less than 0!");
}
while (low + 1 < up)
{
int mid = low + (up - low) / 2;
int k = mid * mid;
if (k == value)
{
return mid;
}
else if (k > value)
{
up = mid;
}
else
{
low = mid;
}
}
return low;
}
static int SearchSqrt1(int value)
{
int bit = 1 << 15;
int res = 0;
if (value < 0)
{
throw new ArgumentException("Input value cannot be less than 0!");
}
while (bit > 0)
{
if (bit <= value)
{
int k = res + bit;
long square = k * k;
if (square < value)
{
res += bit;
}
else if (square == value)
{
return k;
}
}
bit >>= 1;
}
return res;
}
static void Main()
{
for (int i = 0; i < 101; ++i)
{
Console.WriteLine("The sqrt of {0, 5} is {1, 5}", i, SearchSqrt1(i));
}
}
}
}
Output
The sqrt of 0 is 0
The sqrt of 1 is 1
The sqrt of 2 is 1
The sqrt of 3 is 1
The sqrt of 4 is 2
The sqrt of 5 is 2
The sqrt of 6 is 2
The sqrt of 7 is 2
The sqrt of 8 is 2
The sqrt of 9 is 3
The sqrt of 10 is 3
The sqrt of 11 is 3
The sqrt of 12 is 3
The sqrt of 13 is 3
The sqrt of 14 is 3
The sqrt of 15 is 3
The sqrt of 16 is 4
The sqrt of 17 is 4
The sqrt of 18 is 4
The sqrt of 19 is 4
The sqrt of 20 is 4
The sqrt of 21 is 4
The sqrt of 22 is 4
The sqrt of 23 is 4
The sqrt of 24 is 4
The sqrt of 25 is 5
The sqrt of 26 is 5
The sqrt of 27 is 5
The sqrt of 28 is 5
The sqrt of 29 is 5
The sqrt of 30 is 5
The sqrt of 31 is 5
The sqrt of 32 is 5
The sqrt of 33 is 5
The sqrt of 34 is 5
The sqrt of 35 is 5
The sqrt of 36 is 6
The sqrt of 37 is 6
The sqrt of 38 is 6
The sqrt of 39 is 6
The sqrt of 40 is 6
The sqrt of 41 is 6
The sqrt of 42 is 6
The sqrt of 43 is 6
The sqrt of 44 is 6
The sqrt of 45 is 6
The sqrt of 46 is 6
The sqrt of 47 is 6
The sqrt of 48 is 6
The sqrt of 49 is 7
The sqrt of 50 is 7
The sqrt of 51 is 7
The sqrt of 52 is 7
The sqrt of 53 is 7
The sqrt of 54 is 7
The sqrt of 55 is 7
The sqrt of 56 is 7
The sqrt of 57 is 7
The sqrt of 58 is 7
The sqrt of 59 is 7
The sqrt of 60 is 7
The sqrt of 61 is 7
The sqrt of 62 is 7
The sqrt of 63 is 7
The sqrt of 64 is 8
The sqrt of 65 is 8
The sqrt of 66 is 8
The sqrt of 67 is 8
The sqrt of 68 is 8
The sqrt of 69 is 8
The sqrt of 70 is 8
The sqrt of 71 is 8
The sqrt of 72 is 8
The sqrt of 73 is 8
The sqrt of 74 is 8
The sqrt of 75 is 8
The sqrt of 76 is 8
The sqrt of 77 is 8
The sqrt of 78 is 8
The sqrt of 79 is 8
The sqrt of 80 is 8
The sqrt of 81 is 9
The sqrt of 82 is 9
The sqrt of 83 is 9
The sqrt of 84 is 9
The sqrt of 85 is 9
The sqrt of 86 is 9
The sqrt of 87 is 9
The sqrt of 88 is 9
The sqrt of 89 is 9
The sqrt of 90 is 9
The sqrt of 91 is 9
The sqrt of 92 is 9
The sqrt of 93 is 9
The sqrt of 94 is 9
The sqrt of 95 is 9
The sqrt of 96 is 9
The sqrt of 97 is 9
The sqrt of 98 is 9
The sqrt of 99 is 9
The sqrt of 100 is 10