Created
May 8, 2014 06:04
-
-
Save sarathsp06/c98bae04776b68676b3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
finding squre root with log(n) complexity