Skip to content

Instantly share code, notes, and snippets.

@adler3d
Last active March 30, 2017 13:09
Show Gist options
  • Save adler3d/297dddd760c959d11ef348d1182d9528 to your computer and use it in GitHub Desktop.
Save adler3d/297dddd760c959d11ef348d1182d9528 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main() {
auto sqr=[](int x){return x*x;};
auto sqrt=[sqr](int x){
auto center=[](int a,int b){return (a+b)>>1;};
int low=0<<(sizeof(x)*8/2);
int top=1<<(sizeof(x)*8/2);
cout<<"top = "<<top<<endl;
for(int i=0;i<32;i++){
cout<<"low = "<<low<<" top = "<<top<<endl;
int c=center(low,top);cout<<c<<endl;
(c*c<=x?low:top)=c;
if(sqr(c+0)<=x)if(sqr(c+1)>x)return c;
}
return -1;
};
cout<<"result = "<<sqrt(sqr(31000))<<endl;;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment