-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Add integer square root, math.isqrt #81068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The integer square root[1] is a common number-theoretic primitive, useful for example in primality testing. This is something I've had to code up myself multiple times, and is also something that's quite easy to get wrong, or implement in a way that's inefficient for large inputs. I propose adding a math module function Negative inputs should give a ValueError; non-integer inputs (including I'll create a PR shortly with a basic implementation; optimizations can happen later. |
+1, but I propose to add it to the new imath module and move also factorial() and gcd() to it. New binomial() or comb() function (bpo-35431) should be added in imath too. |
FTR (not for Serhiy, but for others reading this), here's the previous discussion of the possibility of adding an imath module. https://mail.python.org/pipermail/python-ideas/2018-July/051917.html While I'm happy for this to go in either math or a new imath module, I'm a bit sceptical that at this point we're going to get a useful imath module together in time for 3.8 (and I don't have bandwidth to do the imath work myself). How about we put it in math for now, and if we get around to imath before the 3.8b1, I'm happy to have it move from math to imath? |
I am wondering whether int(sqrt(float(n))) can be used as a good initial approximation. |
It can, but I'd prefer to avoid floating-point arithmetic (we don't have any guarantees about the accuracy of sqrt, so you'd always need a check and a fallback for the case where sqrt isn't accurate enough), and there are other purely-integer-based ways to produce a fast initial approximation. I do have some plans to add optimizations, but wanted to get the basic algorithm in first. |
What do you think about adding two integer square root functions -- for the largest int |
I'd spell that as |
Some more discussion of possible algorithms and variations here: https://github.com/mdickinson/snippets/blob/master/notebooks/Integer%20square%20roots.ipynb (not sure why the LaTeX isn't all rendering properly at the bottom in the GitHub view; it's fine in my local notebook). |
Could you please add this in the documentation? |
Yes, definitely! |
Yes please for this! The two usual versions are isqrt and nsqrt: isqrt(n) = largest integer ≤ √n nsqrt(n) = closest integer to √n although to be honest I'm not sure what use cases there are for nsqrt. |
+1 from me! I'm tired of re-inventing this too :-) Agree with every point Mark made. Just in passing, noting a triviality: for the ceiling, But I've never had a use for the ceiling here, or for "nearest" - just the floor. Also for |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: