Timeline for answer to How do I calculate square root in Python? by Uncle Dino
Current License: CC BY-SA 4.0
Post Revisions
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 15, 2023 at 6:05 | comment | added | Hans Brende |
@UncleDino my answer has similar or better precision (i.e., arbitrary precision)... while being correctly rounded (rather than truncated à la isqrt): stackoverflow.com/a/77295191/2599133
|
|
| Jan 28, 2022 at 17:14 | comment | added | PM 2Ring | Yes, Newton's method is generally faster. Bisection increases the precision of the result by 1 bit per loop. Newton's method, starting with a reasonable first approximation, (roughly) doubles the precision per loop. With a bad 1st approximation, Newton's method degrades to bisection until the value gets close enough to the root. Python floats have 53 bits of precision. | |
| Jan 25, 2022 at 20:27 | comment | added | wjandrea |
It also still doesn't work for d=0 -> (1, False). Maybe zero could be considered a special case, IDK, but math.isqrt() returns 0, which fits your criterion (left**2==d).
|
|
| Jan 25, 2022 at 20:22 | comment | added | wjandrea |
After the edit, it doesn't work for d=4 -> (1, False). Why would you use this implementation instead of math.isqrt(), which always returns the correct result and is implemented in C? (for CPython). Either way, it's still not what the question is looking for, which is a real number result.
|
|
| Jan 25, 2022 at 14:32 | history | edited | Uncle Dino | CC BY-SA 4.0 |
added EDIT2
|
| Jan 25, 2022 at 14:22 | history | edited | Uncle Dino | CC BY-SA 4.0 |
added 1759 characters in body
|
| Jan 24, 2022 at 19:29 | comment | added | wjandrea |
It also doesn't work for d=1 or d=0.
|
|
| Jan 24, 2022 at 19:25 | comment | added | wjandrea |
This doesn't work for d=2: it never finishes. Even if it did finish, it'd return 1 (IIUC), which is not what the question is looking for: "√2 = 1.4142". I hate to downvote a potentially useful answer, but this just doesn't answer the question. You might want to post this on Integer square root in python instead, after fixing the algo.
|
|
| Jan 22, 2022 at 0:18 | comment | added | Uncle Dino | @wjandrea yes, this implementation does that. The general idea can be used with floats as well to achieve arbitrary precision (bound by float's precision), but then the other answer's methods are better in most terms. | |
| Jan 21, 2022 at 21:42 | comment | added | Eric Duminil |
Well done, BTW. It even works fine for int_squareroot(int('123456789'*100)**2).
|
|
| Jan 21, 2022 at 21:42 | comment | added | Eric Duminil |
@wjandrea: The above function seems to answer the question "what's the largest integer whose square fits in d?" It's similar to n // p and "how many times can p fit in d"?
|
|
| Jan 21, 2022 at 21:28 | comment | added | wjandrea |
If d is not a perfect square, the result will always be lower than its actual root, right? There's an existing question about that: Integer square root in python. You might want to post this solution there as well. There's also How to find integer nth roots?.
|
|
| Jan 21, 2022 at 17:12 | history | answered | Uncle Dino | CC BY-SA 4.0 |