Closest integer to the square root of the positive integer n. a is an initial approximation to the square root. Any positive integer will do for a, but the closer a is to the square root of n the faster convergence will be.
(n, a)
| 5705 | return None if val_n < -e else n // 10**-e |
| 5706 | |
| 5707 | def _sqrt_nearest(n, a): |
| 5708 | """Closest integer to the square root of the positive integer n. a is |
| 5709 | an initial approximation to the square root. Any positive integer |
| 5710 | will do for a, but the closer a is to the square root of n the |
| 5711 | faster convergence will be. |
| 5712 | |
| 5713 | """ |
| 5714 | if n <= 0 or a <= 0: |
| 5715 | raise ValueError("Both arguments to _sqrt_nearest should be positive.") |
| 5716 | |
| 5717 | b=0 |
| 5718 | while a != b: |
| 5719 | b, a = a, a--n//a>>1 |
| 5720 | return a |
| 5721 | |
| 5722 | def _rshift_nearest(x, shift): |
| 5723 | """Given an integer x and a nonnegative integer shift, return closest |