sympy / sympy

A computer algebra system written in pure Python
https://sympy.org/
Other
13.05k stars 4.46k forks source link

isprime can be faster #6908

Closed asmeurer closed 4 years ago

asmeurer commented 11 years ago
See http://www.johndcook.com/blog/2013/01/17/narcissus-prime-in-python/ . The code isprime(int("1808010808"*1560 + "1")) should return within a couple of minutes.

Original issue for #6908: http://code.google.com/p/sympy/issues/detail?id=3809 Original author: https://code.google.com/u/asmeurer@gmail.com/

skirpichev commented 9 years ago

2209 is still open

asmeurer commented 9 years ago

But I think it's abandoned. It you want to pick up the work there feel free.

danaj commented 9 years ago

@Amitjha1412, @skirpichev is correct that this should be fixed by 2209. I will get that completed in early February.

gschintgen commented 5 years ago

Could be closed? #2209 superseded by #10943, which has been merged.

asmeurer commented 5 years ago

I just tested on master and the original isprime(int("1808010808"*1560 + "1")) took 28min 41s to finish on my laptop. I don't know how long it took before. Presumably we can get it faster than this still.

We should also go through the discussion here and see if all the ideas that were discussed were either implemented or decided that they would not actually help the implementation. If someone wants to go through this and write out the list of things that were mentioned, that would be helpful.

gschintgen commented 5 years ago

I just tested on master and the original isprime(int("1808010808"*1560 + "1")) took 28min 41s to finish on my laptop. I don't know how long it took before. Presumably we can get it faster than this still.

We should also go through the discussion here and see if all the ideas that were discussed were either implemented or decided that they would not actually help the implementation. If someone wants to go through this and write out the list of things that were mentioned, that would be helpful.

Hi, I may have been a bit hasty in suggesting to close this issue, but only slightly so:

>>> start=time.time(); isprime(int("1808010808"*1560 + "1")); end=time.time(); print(end-start)
True
91.85459184646606

but that's with gmpy2 installed. Anyway here's the requested summary of the discussion:

So SymPy's pure-python isprime seems to have come down from hours to half an hour (not confirmed personally, may have been on very different hardware), while with gmpy2 installed it's at most a couple of minutes.

IMHO there's only a tiny missing piece: the current isprime docstring mentions BSPW, but omits to clarify that this requires gmpy2.

asmeurer commented 5 years ago

Is BSPW something we can implement in pure Python for when we don't have gmpy2 to speed up that case?

gschintgen commented 5 years ago

Is BSPW something we can implement in pure Python for when we don't have gmpy2 to speed up that case?

Hmm, it may well be that the new isprime actually implements BSPW as well! Might have missed that.

gschintgen commented 5 years ago

Is BSPW something we can implement in pure Python for when we don't have gmpy2 to speed up that case?

Yep, is already implemented: https://github.com/sympy/sympy/blob/56dceb1974848d97636f2f41c0893c738380e2e2/sympy/ntheory/primetest.py#L628-L641