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
introduce primes() to generate kth to nth prime numbers #20830
Conversation
✅ Hi, I am the SymPy bot (v161). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.9. Click here to see the pull request description that was parsed.
|
Ready for review. |
As I noted on the issue, I support this in principle. However, I think the API here needs work
|
sympy/ntheory/generate.py
Outdated
@@ -1005,3 +1005,34 @@ def compositepi(n): | |||
if n < 4: | |||
return 0 | |||
return n - primepi(n) - 1 | |||
|
|||
def nprime(nth, kth=1): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameters should just be n
and k
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asmeurer With parameters being just n
and k
people may get confused like you did #20830 (comment)
sympy/ntheory/generate.py
Outdated
""" | ||
n, k = map(int, (nth, kth)) | ||
if n < 1: | ||
raise ValueError("nth must be a positive integer; nprime(1) == 2") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example at the end of the error message is confusing.
sympy/ntheory/generate.py
Outdated
if k < 1: | ||
raise ValueError("kth must be a positive integer; nprime(3, 2) == [3, 5]") | ||
if k>n: | ||
raise ValueError("kth can't be greater than nth") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I this case it could just return an empty list.
sympy/ntheory/generate.py
Outdated
>>> nprime(7, 3) | ||
[5, 7, 11, 13, 17] | ||
""" | ||
n, k = map(int, (nth, kth)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use as_int here.
Oh never mind I was confusing the kth prime with the prime k. You might want to adjust the example so it is clear it is referring to the kth primes and not k as a prime. I would suggest using larger inputs for the 2-arg example, like |
sympy/ntheory/generate.py
Outdated
@@ -1005,3 +1005,34 @@ def compositepi(n): | |||
if n < 4: | |||
return 0 | |||
return n - primepi(n) - 1 | |||
|
|||
def nprime(nth, kth=1): | |||
""" Returns kth to nth prime numbers(both inclusive). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow the docstring style guide. https://docs.sympy.org/latest/documentation-style-guide.html
Please see issue page #19118 for alternate solution using existing |
sympy/ntheory/generate.py
Outdated
if k>n: | ||
return [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if k>n: | |
return [] | |
if k > n: | |
raise ValueError('k should not be less than n') |
sympy/ntheory/tests/test_generate.py
Outdated
raises(ValueError, lambda: nprimes(0)) | ||
raises(ValueError, lambda: nprimes(10, 0)) | ||
raises(ValueError, lambda: nprimes(10,-5)) | ||
assert nprimes(10, 2) == [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this is not working like a range
function I think we can be more strict in requiring that k >=n
.
Also, I like the name primes
better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smichr nprimes
seems to be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The n
does not provide more clarity than the pluralizing s
. I prefer one of the two options I described in the issue and am barely +1/2 because there is already a clear way to do this with primerange
. But something that makes me close to +1 is getting a list rather than a generator: list(primerange(prime(10)+1))
vs primes(10)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make it primes
Co-authored-by: Christopher Smith <smichr@gmail.com>
Co-authored-by: Christopher Smith <smichr@gmail.com>
Co-authored-by: Christopher Smith <smichr@gmail.com>
Co-authored-by: Christopher Smith <smichr@gmail.com>
@oscarbenjamin , I imagine that you would prefer a separate function like this instead of a polymorphic @sidhu1012 , documentation showing how to get a list of the first n primes in other functions should be updated...I think you can regex search for |
It seems to me that the first issue is the documentation so I wouldn't say that this "fixes" the linked issue. It's clear that the documentation needs to be improved but it's not clear that we need this function. As @smichr says this is already quite doable with the existing primitives. The most useful thing we could add are "see also" references to all related functions linking to each of the other functions and examples in the docstrings showing how to do common things that users would want to do. If we had those then I'm not sure that there would be any need for this function. Also is |
The |
ping @asmeurer who had some favorable inclinations |
But why does anyone specifically need this one-based indexing? It is better I think for sympy to just give something simple that is easy to understand and useful e.g. If the function does something clever to compute |
agree. And My only concern is that given this method people start using some bad habits. I'm not sure how those would play out but others may think of some. So I am slow to commit on this one. |
>>> primes = lambda k,n=1: list(primerange(prime(min(k,n)), prime(max(k,n))+1))
>>> primes(4)
[2, 3, 5, 7]
>>> primes(3,4)
[5, 7]
>>> primes(4,3)
[5, 7] |
References to other Issues or PRs
Fixes #19118
Brief description of what is fixed or changed
Other comments
Release Notes
primes()
to generate kth to nth prime numbers or first n prime numbers.