fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. auto sqr=[](int x){return x*x;};
  6. auto sqrt=[sqr](int x){
  7. auto center=[](int a,int b){return (a+b)>>1;};
  8. int low=0<<(sizeof(x)*8/2);
  9. int top=1<<(sizeof(x)*8/2);
  10. cout<<"top = "<<top<<endl;
  11. for(int i=0;i<32;i++){
  12. cout<<"low = "<<low<<" top = "<<top<<endl;
  13. int c=center(low,top);cout<<c<<endl;
  14. (c*c<=x?low:top)=c;
  15. if(sqr(c+0)<=x)if(sqr(c+1)>x)return c;
  16. }
  17. return -1;
  18. };
  19. cout<<"result = "<<sqrt(sqr(31000))<<endl;;
  20. return 0;
  21. }
Success #stdin #stdout 0s 15224KB
stdin
Standard input is empty
stdout
top = 65536
low = 0     top = 65536
32768
low = 0     top = 32768
16384
low = 16384     top = 32768
24576
low = 24576     top = 32768
28672
low = 28672     top = 32768
30720
low = 30720     top = 32768
31744
low = 30720     top = 31744
31232
low = 30720     top = 31232
30976
low = 30976     top = 31232
31104
low = 30976     top = 31104
31040
low = 30976     top = 31040
31008
low = 30976     top = 31008
30992
low = 30992     top = 31008
31000
result = 31000