Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save KodeSeeker/fd1fdc0eb25f38be8789 to your computer and use it in GitHub Desktop.

Select an option

Save KodeSeeker/fd1fdc0eb25f38be8789 to your computer and use it in GitHub Desktop.
public int SqRoot(int num)
{
if(num<0 || num>INTEGER.MAX_VALUE)
throw new Exception();
int high=num;
int low=0;
int mid =Math.floor((low+high)/2);
while(low+1<high)
{
if(mid*mid==num)
return mid;
else if (mid>num)
high=mid;
else
low=mid;
}
return low;// if mid*mid never becomes num.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment