primetest.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. """
  2. Primality testing
  3. """
  4. from sympy.core.numbers import igcd
  5. from sympy.core.power import integer_nthroot
  6. from sympy.core.sympify import sympify
  7. from sympy.external.gmpy import HAS_GMPY
  8. from sympy.utilities.misc import as_int
  9. from mpmath.libmp import bitcount as _bitlength
  10. def _int_tuple(*i):
  11. return tuple(int(_) for _ in i)
  12. def is_euler_pseudoprime(n, b):
  13. """Returns True if n is prime or an Euler pseudoprime to base b, else False.
  14. Euler Pseudoprime : In arithmetic, an odd composite integer n is called an
  15. euler pseudoprime to base a, if a and n are coprime and satisfy the modular
  16. arithmetic congruence relation :
  17. a ^ (n-1)/2 = + 1(mod n) or
  18. a ^ (n-1)/2 = - 1(mod n)
  19. (where mod refers to the modulo operation).
  20. Examples
  21. ========
  22. >>> from sympy.ntheory.primetest import is_euler_pseudoprime
  23. >>> is_euler_pseudoprime(2, 5)
  24. True
  25. References
  26. ==========
  27. .. [1] https://en.wikipedia.org/wiki/Euler_pseudoprime
  28. """
  29. from sympy.ntheory.factor_ import trailing
  30. if not mr(n, [b]):
  31. return False
  32. n = as_int(n)
  33. r = n - 1
  34. c = pow(b, r >> trailing(r), n)
  35. if c == 1:
  36. return True
  37. while True:
  38. if c == n - 1:
  39. return True
  40. c = pow(c, 2, n)
  41. if c == 1:
  42. return False
  43. def is_square(n, prep=True):
  44. """Return True if n == a * a for some integer a, else False.
  45. If n is suspected of *not* being a square then this is a
  46. quick method of confirming that it is not.
  47. Examples
  48. ========
  49. >>> from sympy.ntheory.primetest import is_square
  50. >>> is_square(25)
  51. True
  52. >>> is_square(2)
  53. False
  54. References
  55. ==========
  56. .. [1] https://mersenneforum.org/showpost.php?p=110896
  57. See Also
  58. ========
  59. sympy.core.power.integer_nthroot
  60. """
  61. if prep:
  62. n = as_int(n)
  63. if n < 0:
  64. return False
  65. if n in (0, 1):
  66. return True
  67. # def magic(n):
  68. # s = {x**2 % n for x in range(n)}
  69. # return sum(1 << bit for bit in s)
  70. # >>> print(hex(magic(128)))
  71. # 0x2020212020202130202021202030213
  72. # >>> print(hex(magic(99)))
  73. # 0x209060049048220348a410213
  74. # >>> print(hex(magic(91)))
  75. # 0x102e403012a0c9862c14213
  76. # >>> print(hex(magic(85)))
  77. # 0x121065188e001c46298213
  78. if not 0x2020212020202130202021202030213 & (1 << (n & 127)):
  79. return False # e.g. 2, 3
  80. m = n % (99 * 91 * 85)
  81. if not 0x209060049048220348a410213 & (1 << (m % 99)):
  82. return False # e.g. 17, 68
  83. if not 0x102e403012a0c9862c14213 & (1 << (m % 91)):
  84. return False # e.g. 97, 388
  85. if not 0x121065188e001c46298213 & (1 << (m % 85)):
  86. return False # e.g. 793, 1408
  87. # n is either:
  88. # a) odd = 4*even + 1 (and square if even = k*(k + 1))
  89. # b) even with
  90. # odd multiplicity of 2 --> not square, e.g. 39040
  91. # even multiplicity of 2, e.g. 4, 16, 36, ..., 16324
  92. # removal of factors of 2 to give an odd, and rejection if
  93. # any(i%2 for i in divmod(odd - 1, 4))
  94. # will give an odd number in form 4*even + 1.
  95. # Use of `trailing` to check the power of 2 is not done since it
  96. # does not apply to a large percentage of arbitrary numbers
  97. # and the integer_nthroot is able to quickly resolve these cases.
  98. return integer_nthroot(n, 2)[1]
  99. def _test(n, base, s, t):
  100. """Miller-Rabin strong pseudoprime test for one base.
  101. Return False if n is definitely composite, True if n is
  102. probably prime, with a probability greater than 3/4.
  103. """
  104. # do the Fermat test
  105. b = pow(base, t, n)
  106. if b == 1 or b == n - 1:
  107. return True
  108. else:
  109. for j in range(1, s):
  110. b = pow(b, 2, n)
  111. if b == n - 1:
  112. return True
  113. # see I. Niven et al. "An Introduction to Theory of Numbers", page 78
  114. if b == 1:
  115. return False
  116. return False
  117. def mr(n, bases):
  118. """Perform a Miller-Rabin strong pseudoprime test on n using a
  119. given list of bases/witnesses.
  120. References
  121. ==========
  122. .. [1] Richard Crandall & Carl Pomerance (2005), "Prime Numbers:
  123. A Computational Perspective", Springer, 2nd edition, 135-138
  124. A list of thresholds and the bases they require are here:
  125. https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Deterministic_variants
  126. Examples
  127. ========
  128. >>> from sympy.ntheory.primetest import mr
  129. >>> mr(1373651, [2, 3])
  130. False
  131. >>> mr(479001599, [31, 73])
  132. True
  133. """
  134. from sympy.ntheory.factor_ import trailing
  135. from sympy.polys.domains import ZZ
  136. n = as_int(n)
  137. if n < 2:
  138. return False
  139. # remove powers of 2 from n-1 (= t * 2**s)
  140. s = trailing(n - 1)
  141. t = n >> s
  142. for base in bases:
  143. # Bases >= n are wrapped, bases < 2 are invalid
  144. if base >= n:
  145. base %= n
  146. if base >= 2:
  147. base = ZZ(base)
  148. if not _test(n, base, s, t):
  149. return False
  150. return True
  151. def _lucas_sequence(n, P, Q, k):
  152. """Return the modular Lucas sequence (U_k, V_k, Q_k).
  153. Given a Lucas sequence defined by P, Q, returns the kth values for
  154. U and V, along with Q^k, all modulo n. This is intended for use with
  155. possibly very large values of n and k, where the combinatorial functions
  156. would be completely unusable.
  157. The modular Lucas sequences are used in numerous places in number theory,
  158. especially in the Lucas compositeness tests and the various n + 1 proofs.
  159. Examples
  160. ========
  161. >>> from sympy.ntheory.primetest import _lucas_sequence
  162. >>> N = 10**2000 + 4561
  163. >>> sol = U, V, Qk = _lucas_sequence(N, 3, 1, N//2); sol
  164. (0, 2, 1)
  165. """
  166. D = P*P - 4*Q
  167. if n < 2:
  168. raise ValueError("n must be >= 2")
  169. if k < 0:
  170. raise ValueError("k must be >= 0")
  171. if D == 0:
  172. raise ValueError("D must not be zero")
  173. if k == 0:
  174. return _int_tuple(0, 2, Q)
  175. U = 1
  176. V = P
  177. Qk = Q
  178. b = _bitlength(k)
  179. if Q == 1:
  180. # Optimization for extra strong tests.
  181. while b > 1:
  182. U = (U*V) % n
  183. V = (V*V - 2) % n
  184. b -= 1
  185. if (k >> (b - 1)) & 1:
  186. U, V = U*P + V, V*P + U*D
  187. if U & 1:
  188. U += n
  189. if V & 1:
  190. V += n
  191. U, V = U >> 1, V >> 1
  192. elif P == 1 and Q == -1:
  193. # Small optimization for 50% of Selfridge parameters.
  194. while b > 1:
  195. U = (U*V) % n
  196. if Qk == 1:
  197. V = (V*V - 2) % n
  198. else:
  199. V = (V*V + 2) % n
  200. Qk = 1
  201. b -= 1
  202. if (k >> (b-1)) & 1:
  203. U, V = U + V, V + U*D
  204. if U & 1:
  205. U += n
  206. if V & 1:
  207. V += n
  208. U, V = U >> 1, V >> 1
  209. Qk = -1
  210. else:
  211. # The general case with any P and Q.
  212. while b > 1:
  213. U = (U*V) % n
  214. V = (V*V - 2*Qk) % n
  215. Qk *= Qk
  216. b -= 1
  217. if (k >> (b - 1)) & 1:
  218. U, V = U*P + V, V*P + U*D
  219. if U & 1:
  220. U += n
  221. if V & 1:
  222. V += n
  223. U, V = U >> 1, V >> 1
  224. Qk *= Q
  225. Qk %= n
  226. return _int_tuple(U % n, V % n, Qk)
  227. def _lucas_selfridge_params(n):
  228. """Calculates the Selfridge parameters (D, P, Q) for n. This is
  229. method A from page 1401 of Baillie and Wagstaff.
  230. References
  231. ==========
  232. .. [1] "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
  233. http://mpqs.free.fr/LucasPseudoprimes.pdf
  234. """
  235. from sympy.ntheory.residue_ntheory import jacobi_symbol
  236. D = 5
  237. while True:
  238. g = igcd(abs(D), n)
  239. if g > 1 and g != n:
  240. return (0, 0, 0)
  241. if jacobi_symbol(D, n) == -1:
  242. break
  243. if D > 0:
  244. D = -D - 2
  245. else:
  246. D = -D + 2
  247. return _int_tuple(D, 1, (1 - D)/4)
  248. def _lucas_extrastrong_params(n):
  249. """Calculates the "extra strong" parameters (D, P, Q) for n.
  250. References
  251. ==========
  252. .. [1] OEIS A217719: Extra Strong Lucas Pseudoprimes
  253. https://oeis.org/A217719
  254. .. [1] https://en.wikipedia.org/wiki/Lucas_pseudoprime
  255. """
  256. from sympy.ntheory.residue_ntheory import jacobi_symbol
  257. P, Q, D = 3, 1, 5
  258. while True:
  259. g = igcd(D, n)
  260. if g > 1 and g != n:
  261. return (0, 0, 0)
  262. if jacobi_symbol(D, n) == -1:
  263. break
  264. P += 1
  265. D = P*P - 4
  266. return _int_tuple(D, P, Q)
  267. def is_lucas_prp(n):
  268. """Standard Lucas compositeness test with Selfridge parameters. Returns
  269. False if n is definitely composite, and True if n is a Lucas probable
  270. prime.
  271. This is typically used in combination with the Miller-Rabin test.
  272. References
  273. ==========
  274. - "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
  275. http://mpqs.free.fr/LucasPseudoprimes.pdf
  276. - OEIS A217120: Lucas Pseudoprimes
  277. https://oeis.org/A217120
  278. - https://en.wikipedia.org/wiki/Lucas_pseudoprime
  279. Examples
  280. ========
  281. >>> from sympy.ntheory.primetest import isprime, is_lucas_prp
  282. >>> for i in range(10000):
  283. ... if is_lucas_prp(i) and not isprime(i):
  284. ... print(i)
  285. 323
  286. 377
  287. 1159
  288. 1829
  289. 3827
  290. 5459
  291. 5777
  292. 9071
  293. 9179
  294. """
  295. n = as_int(n)
  296. if n == 2:
  297. return True
  298. if n < 2 or (n % 2) == 0:
  299. return False
  300. if is_square(n, False):
  301. return False
  302. D, P, Q = _lucas_selfridge_params(n)
  303. if D == 0:
  304. return False
  305. U, V, Qk = _lucas_sequence(n, P, Q, n+1)
  306. return U == 0
  307. def is_strong_lucas_prp(n):
  308. """Strong Lucas compositeness test with Selfridge parameters. Returns
  309. False if n is definitely composite, and True if n is a strong Lucas
  310. probable prime.
  311. This is often used in combination with the Miller-Rabin test, and
  312. in particular, when combined with M-R base 2 creates the strong BPSW test.
  313. References
  314. ==========
  315. - "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
  316. http://mpqs.free.fr/LucasPseudoprimes.pdf
  317. - OEIS A217255: Strong Lucas Pseudoprimes
  318. https://oeis.org/A217255
  319. - https://en.wikipedia.org/wiki/Lucas_pseudoprime
  320. - https://en.wikipedia.org/wiki/Baillie-PSW_primality_test
  321. Examples
  322. ========
  323. >>> from sympy.ntheory.primetest import isprime, is_strong_lucas_prp
  324. >>> for i in range(20000):
  325. ... if is_strong_lucas_prp(i) and not isprime(i):
  326. ... print(i)
  327. 5459
  328. 5777
  329. 10877
  330. 16109
  331. 18971
  332. """
  333. from sympy.ntheory.factor_ import trailing
  334. n = as_int(n)
  335. if n == 2:
  336. return True
  337. if n < 2 or (n % 2) == 0:
  338. return False
  339. if is_square(n, False):
  340. return False
  341. D, P, Q = _lucas_selfridge_params(n)
  342. if D == 0:
  343. return False
  344. # remove powers of 2 from n+1 (= k * 2**s)
  345. s = trailing(n + 1)
  346. k = (n+1) >> s
  347. U, V, Qk = _lucas_sequence(n, P, Q, k)
  348. if U == 0 or V == 0:
  349. return True
  350. for r in range(1, s):
  351. V = (V*V - 2*Qk) % n
  352. if V == 0:
  353. return True
  354. Qk = pow(Qk, 2, n)
  355. return False
  356. def is_extra_strong_lucas_prp(n):
  357. """Extra Strong Lucas compositeness test. Returns False if n is
  358. definitely composite, and True if n is a "extra strong" Lucas probable
  359. prime.
  360. The parameters are selected using P = 3, Q = 1, then incrementing P until
  361. (D|n) == -1. The test itself is as defined in Grantham 2000, from the
  362. Mo and Jones preprint. The parameter selection and test are the same as
  363. used in OEIS A217719, Perl's Math::Prime::Util, and the Lucas pseudoprime
  364. page on Wikipedia.
  365. With these parameters, there are no counterexamples below 2^64 nor any
  366. known above that range. It is 20-50% faster than the strong test.
  367. Because of the different parameters selected, there is no relationship
  368. between the strong Lucas pseudoprimes and extra strong Lucas pseudoprimes.
  369. In particular, one is not a subset of the other.
  370. References
  371. ==========
  372. - "Frobenius Pseudoprimes", Jon Grantham, 2000.
  373. https://www.ams.org/journals/mcom/2001-70-234/S0025-5718-00-01197-2/
  374. - OEIS A217719: Extra Strong Lucas Pseudoprimes
  375. https://oeis.org/A217719
  376. - https://en.wikipedia.org/wiki/Lucas_pseudoprime
  377. Examples
  378. ========
  379. >>> from sympy.ntheory.primetest import isprime, is_extra_strong_lucas_prp
  380. >>> for i in range(20000):
  381. ... if is_extra_strong_lucas_prp(i) and not isprime(i):
  382. ... print(i)
  383. 989
  384. 3239
  385. 5777
  386. 10877
  387. """
  388. # Implementation notes:
  389. # 1) the parameters differ from Thomas R. Nicely's. His parameter
  390. # selection leads to pseudoprimes that overlap M-R tests, and
  391. # contradict Baillie and Wagstaff's suggestion of (D|n) = -1.
  392. # 2) The MathWorld page as of June 2013 specifies Q=-1. The Lucas
  393. # sequence must have Q=1. See Grantham theorem 2.3, any of the
  394. # references on the MathWorld page, or run it and see Q=-1 is wrong.
  395. from sympy.ntheory.factor_ import trailing
  396. n = as_int(n)
  397. if n == 2:
  398. return True
  399. if n < 2 or (n % 2) == 0:
  400. return False
  401. if is_square(n, False):
  402. return False
  403. D, P, Q = _lucas_extrastrong_params(n)
  404. if D == 0:
  405. return False
  406. # remove powers of 2 from n+1 (= k * 2**s)
  407. s = trailing(n + 1)
  408. k = (n+1) >> s
  409. U, V, Qk = _lucas_sequence(n, P, Q, k)
  410. if U == 0 and (V == 2 or V == n - 2):
  411. return True
  412. for r in range(1, s):
  413. if V == 0:
  414. return True
  415. V = (V*V - 2) % n
  416. return False
  417. def isprime(n):
  418. """
  419. Test if n is a prime number (True) or not (False). For n < 2^64 the
  420. answer is definitive; larger n values have a small probability of actually
  421. being pseudoprimes.
  422. Negative numbers (e.g. -2) are not considered prime.
  423. The first step is looking for trivial factors, which if found enables
  424. a quick return. Next, if the sieve is large enough, use bisection search
  425. on the sieve. For small numbers, a set of deterministic Miller-Rabin
  426. tests are performed with bases that are known to have no counterexamples
  427. in their range. Finally if the number is larger than 2^64, a strong
  428. BPSW test is performed. While this is a probable prime test and we
  429. believe counterexamples exist, there are no known counterexamples.
  430. Examples
  431. ========
  432. >>> from sympy.ntheory import isprime
  433. >>> isprime(13)
  434. True
  435. >>> isprime(13.0) # limited precision
  436. False
  437. >>> isprime(15)
  438. False
  439. Notes
  440. =====
  441. This routine is intended only for integer input, not numerical
  442. expressions which may represent numbers. Floats are also
  443. rejected as input because they represent numbers of limited
  444. precision. While it is tempting to permit 7.0 to represent an
  445. integer there are errors that may "pass silently" if this is
  446. allowed:
  447. >>> from sympy import Float, S
  448. >>> int(1e3) == 1e3 == 10**3
  449. True
  450. >>> int(1e23) == 1e23
  451. True
  452. >>> int(1e23) == 10**23
  453. False
  454. >>> near_int = 1 + S(1)/10**19
  455. >>> near_int == int(near_int)
  456. False
  457. >>> n = Float(near_int, 10) # truncated by precision
  458. >>> n == int(n)
  459. True
  460. >>> n = Float(near_int, 20)
  461. >>> n == int(n)
  462. False
  463. See Also
  464. ========
  465. sympy.ntheory.generate.primerange : Generates all primes in a given range
  466. sympy.ntheory.generate.primepi : Return the number of primes less than or equal to n
  467. sympy.ntheory.generate.prime : Return the nth prime
  468. References
  469. ==========
  470. - https://en.wikipedia.org/wiki/Strong_pseudoprime
  471. - "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
  472. http://mpqs.free.fr/LucasPseudoprimes.pdf
  473. - https://en.wikipedia.org/wiki/Baillie-PSW_primality_test
  474. """
  475. try:
  476. n = as_int(n)
  477. except ValueError:
  478. return False
  479. # Step 1, do quick composite testing via trial division. The individual
  480. # modulo tests benchmark faster than one or two primorial igcds for me.
  481. # The point here is just to speedily handle small numbers and many
  482. # composites. Step 2 only requires that n <= 2 get handled here.
  483. if n in [2, 3, 5]:
  484. return True
  485. if n < 2 or (n % 2) == 0 or (n % 3) == 0 or (n % 5) == 0:
  486. return False
  487. if n < 49:
  488. return True
  489. if (n % 7) == 0 or (n % 11) == 0 or (n % 13) == 0 or (n % 17) == 0 or \
  490. (n % 19) == 0 or (n % 23) == 0 or (n % 29) == 0 or (n % 31) == 0 or \
  491. (n % 37) == 0 or (n % 41) == 0 or (n % 43) == 0 or (n % 47) == 0:
  492. return False
  493. if n < 2809:
  494. return True
  495. if n < 31417:
  496. return pow(2, n, n) == 2 and n not in [7957, 8321, 13747, 18721, 19951, 23377]
  497. # bisection search on the sieve if the sieve is large enough
  498. from sympy.ntheory.generate import sieve as s
  499. if n <= s._list[-1]:
  500. l, u = s.search(n)
  501. return l == u
  502. # If we have GMPY2, skip straight to step 3 and do a strong BPSW test.
  503. # This should be a bit faster than our step 2, and for large values will
  504. # be a lot faster than our step 3 (C+GMP vs. Python).
  505. if HAS_GMPY == 2:
  506. from gmpy2 import is_strong_prp, is_strong_selfridge_prp
  507. return is_strong_prp(n, 2) and is_strong_selfridge_prp(n)
  508. # Step 2: deterministic Miller-Rabin testing for numbers < 2^64. See:
  509. # https://miller-rabin.appspot.com/
  510. # for lists. We have made sure the M-R routine will successfully handle
  511. # bases larger than n, so we can use the minimal set.
  512. # In September 2015 deterministic numbers were extended to over 2^81.
  513. # https://arxiv.org/pdf/1509.00864.pdf
  514. # https://oeis.org/A014233
  515. if n < 341531:
  516. return mr(n, [9345883071009581737])
  517. if n < 885594169:
  518. return mr(n, [725270293939359937, 3569819667048198375])
  519. if n < 350269456337:
  520. return mr(n, [4230279247111683200, 14694767155120705706, 16641139526367750375])
  521. if n < 55245642489451:
  522. return mr(n, [2, 141889084524735, 1199124725622454117, 11096072698276303650])
  523. if n < 7999252175582851:
  524. return mr(n, [2, 4130806001517, 149795463772692060, 186635894390467037, 3967304179347715805])
  525. if n < 585226005592931977:
  526. return mr(n, [2, 123635709730000, 9233062284813009, 43835965440333360, 761179012939631437, 1263739024124850375])
  527. if n < 18446744073709551616:
  528. return mr(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022])
  529. if n < 318665857834031151167461:
  530. return mr(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37])
  531. if n < 3317044064679887385961981:
  532. return mr(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])
  533. # We could do this instead at any point:
  534. #if n < 18446744073709551616:
  535. # return mr(n, [2]) and is_extra_strong_lucas_prp(n)
  536. # Here are tests that are safe for MR routines that don't understand
  537. # large bases.
  538. #if n < 9080191:
  539. # return mr(n, [31, 73])
  540. #if n < 19471033:
  541. # return mr(n, [2, 299417])
  542. #if n < 38010307:
  543. # return mr(n, [2, 9332593])
  544. #if n < 316349281:
  545. # return mr(n, [11000544, 31481107])
  546. #if n < 4759123141:
  547. # return mr(n, [2, 7, 61])
  548. #if n < 105936894253:
  549. # return mr(n, [2, 1005905886, 1340600841])
  550. #if n < 31858317218647:
  551. # return mr(n, [2, 642735, 553174392, 3046413974])
  552. #if n < 3071837692357849:
  553. # return mr(n, [2, 75088, 642735, 203659041, 3613982119])
  554. #if n < 18446744073709551616:
  555. # return mr(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022])
  556. # Step 3: BPSW.
  557. #
  558. # Time for isprime(10**2000 + 4561), no gmpy or gmpy2 installed
  559. # 44.0s old isprime using 46 bases
  560. # 5.3s strong BPSW + one random base
  561. # 4.3s extra strong BPSW + one random base
  562. # 4.1s strong BPSW
  563. # 3.2s extra strong BPSW
  564. # Classic BPSW from page 1401 of the paper. See alternate ideas below.
  565. return mr(n, [2]) and is_strong_lucas_prp(n)
  566. # Using extra strong test, which is somewhat faster
  567. #return mr(n, [2]) and is_extra_strong_lucas_prp(n)
  568. # Add a random M-R base
  569. #import random
  570. #return mr(n, [2, random.randint(3, n-1)]) and is_strong_lucas_prp(n)
  571. def is_gaussian_prime(num):
  572. r"""Test if num is a Gaussian prime number.
  573. References
  574. ==========
  575. .. [1] https://oeis.org/wiki/Gaussian_primes
  576. """
  577. num = sympify(num)
  578. a, b = num.as_real_imag()
  579. a = as_int(a, strict=False)
  580. b = as_int(b, strict=False)
  581. if a == 0:
  582. b = abs(b)
  583. return isprime(b) and b % 4 == 3
  584. elif b == 0:
  585. a = abs(a)
  586. return isprime(a) and a % 4 == 3
  587. return isprime(a**2 + b**2)