Skip to content

Instantly share code, notes, and snippets.

@sarathsp06
Created May 8, 2014 06:04
Show Gist options
  • Save sarathsp06/c98bae04776b68676b3b to your computer and use it in GitHub Desktop.
Save sarathsp06/c98bae04776b68676b3b to your computer and use it in GitHub Desktop.
#include<iostream>
#define sqr(a) ((a)*(a))
using namespace std;
unsigned long sqrt(unsigned long a){
unsigned long l=0,u=a,mid = l+(u-l)/2;
while(mid*mid != a){
if(sqr(mid) < a){
if(sqr(mid+1)>a){
return mid;
}
l=mid+1;
}
else{
if(sqr(mid-1)<a){
return mid;
}
u=mid-1;
}
mid = l+(u-l)/2;
}
return mid;
}
int main(){
unsigned long a;
cin>>a;
cout<<"\nSQUARE ROOT of "<<a<<"is "<<sqrt(a)<<"\n";
return 0;
}
@sarathsp06
Copy link
Author

finding squre root with log(n) complexity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment