https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method

image.png
image.png
image.png

Example

To calculate √S, where S = 125348, to six significant figures, use the rough estimation method above to get
image.png
Therefore, √125348 ≈ 354.045.

Uniswap 中用法

  1. // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
  2. function sqrt(uint y) internal pure returns (uint z) {
  3. if (y > 3) {
  4. z = y;
  5. uint x = y / 2 + 1;
  6. while (x < z) {
  7. z = x;
  8. x = (y / x + x) / 2;
  9. }
  10. } else if (y != 0) {
  11. z = 1;
  12. }
  13. }