fork download
  1. #include<iostream>
  2. #include<iomanip>
  3. #include <cmath>
  4. using namespace std;
  5. float my_sqrt(long long n,int p)
  6. {
  7. int start = 0;
  8. int end = n;
  9. float ans;
  10. int mid;
  11. while(start <= end)
  12. {
  13. mid = (start+end)/2;
  14. if(mid*mid < n)
  15. start = mid + 1;
  16. else if(mid*mid > n)
  17. end = mid - 1;
  18. else
  19. {
  20. ans = mid;
  21. break;
  22. }
  23. }
  24. float increment = 0.1;
  25. for(int i = 0; i < p;i++)
  26. {
  27. while(ans * ans <= n)
  28. {
  29. ans += increment;
  30. }
  31. ans -= increment;
  32. increment /= 10;
  33. }
  34. return ans;
  35. }
  36. int main()
  37. {
  38. long long int n;
  39. for(int i = 0; i < 100; ++i)
  40. {
  41. cout << fixed << setprecision(4) << sqrt(i) << " ";
  42. cout << fixed << setprecision(4) << my_sqrt(i,4) << " ";
  43. cout << fixed << setprecision(6) << my_sqrt(i,4) << endl;
  44. }
  45. }
Success #stdin #stdout 0s 4316KB
stdin
Standard input is empty
stdout
0.0000 0.0000 0.000000
1.0000 1.0000 1.000000
1.4142 1.4142 1.414200
1.7321 1.7320 1.732000
2.0000 2.0000 2.000000
2.2361 2.2360 2.236000
2.4495 2.4494 2.449399
2.6458 2.6457 2.645699
2.8284 2.8284 2.828398
3.0000 3.0000 3.000000
3.1623 3.1622 3.162199
3.3166 3.3166 3.316598
3.4641 3.4641 3.464098
3.6056 3.6055 3.605498
3.7417 3.7416 3.741598
3.8730 3.8729 3.872897
4.0000 4.0000 4.000000
4.1231 4.1231 4.123099
4.2426 4.2426 4.242600
4.3589 4.3588 4.358800
4.4721 4.4721 4.472100
4.5826 4.5825 4.582500
4.6904 4.6904 4.690401
4.7958 4.7958 4.795801
4.8990 4.8989 4.898901
5.0000 5.0000 5.000000
5.0990 5.0990 5.098999
5.1962 5.1961 5.196099
5.2915 5.2915 5.291500
5.3852 5.3851 5.385099
5.4772 5.4772 5.477199
5.5678 5.5677 5.567699
5.6569 5.6568 5.656799
5.7446 5.7445 5.744498
5.8310 5.8309 5.830899
5.9161 5.9160 5.915997
6.0000 6.0000 6.000000
6.0828 6.0827 6.082699
6.1644 6.1644 6.164398
6.2450 6.2450 6.244997
6.3246 6.3245 6.324497
6.4031 6.4031 6.403096
6.4807 6.4807 6.480699
6.5574 6.5574 6.557397
6.6332 6.6332 6.633197
6.7082 6.7082 6.708196
6.7823 6.7823 6.782298
6.8557 6.8556 6.855597
6.9282 6.9282 6.928196
7.0000 7.0000 7.000000
7.0711 7.0710 7.070997
7.1414 7.1414 7.141397
7.2111 7.2111 7.211096
7.2801 7.2801 7.280097
7.3485 7.3484 7.348396
7.4162 7.4162 7.416195
7.4833 7.4833 7.483297
7.5498 7.5498 7.549797
7.6158 7.6157 7.615696
7.6811 7.6811 7.681097
7.7460 7.7459 7.745897
7.8102 7.8102 7.810195
7.8740 7.8740 7.873996
7.9373 7.9372 7.937195
8.0000 8.0000 8.000000
8.0623 8.0622 8.062197
8.1240 8.1240 8.123997
8.1854 8.1853 8.185299
8.2462 8.2462 8.246199
8.3066 8.3066 8.306599
8.3666 8.3666 8.366600
8.4261 8.4261 8.426099
8.4853 8.4852 8.485200
8.5440 8.5440 8.543999
8.6023 8.6023 8.602298
8.6603 8.6602 8.660198
8.7178 8.7177 8.717701
8.7750 8.7749 8.774901
8.8318 8.8317 8.831699
8.8882 8.8881 8.888103
8.9443 8.9442 8.944201
9.0000 9.0000 9.000000
9.0554 9.0553 9.055302
9.1104 9.1104 9.110399
9.1652 9.1651 9.165102
9.2195 9.2195 9.219503
9.2736 9.2736 9.273602
9.3274 9.3273 9.327303
9.3808 9.3808 9.380802
9.4340 9.4339 9.433903
9.4868 9.4868 9.486805
9.5394 9.5393 9.539305
9.5917 9.5916 9.591603
9.6437 9.6436 9.643603
9.6954 9.6953 9.695305
9.7468 9.7467 9.746705
9.7980 9.7979 9.797907
9.8489 9.8488 9.848806
9.8995 9.8994 9.899407
9.9499 9.9498 9.949807