When autoloading a Maxima script, avoid clobbering global state.
[maxima.git] / src / ifactor.lisp
blob5ea18d10e6532a3ee798cccecc6e2b1b7b6e438b
1 ;;;;
2 ;;;; ~*~ IFACTOR ~*~
3 ;;;;
4 ;;;; Maxima integer factorization package.
5 ;;;;
6 ;;;; Copyright: 2005-2015 Andrej Vodopivec, Volker van Nek
7 ;;;; Licence : GPL
8 ;;;;
9 ;;;; - ifactors : factorization of integers
10 ;;;; - primep : probabilistic primality test
11 ;;;; - next_prime : smallest prime > n
12 ;;;; - prev_prime : greatest prime < n
13 ;;;; - primes : list of primes
14 ;;;; - power_mod : fast modular powers
15 ;;;; - inv_mod : modular inverse
18 (in-package :maxima)
19 (macsyma-module ifactor)
21 (defmvar $save_primes nil "Save primes found." boolean)
23 (defmvar $primep_number_of_tests 25 "Number of Miller-Rabin tests." fixnum)
25 (defmvar $pollard_rho_limit 16000 "Limit for pollard-rho factorization depth." fixnum)
26 (defmvar $pollard_pm1_limit 25000 "Limit for pollard-p1 factorization depth." fixnum)
28 (defmvar $pollard_rho_tests 5 "Number of pollard-rho rounds." fixnum)
29 (defmvar $pollard_pm1_tests 3 "Number of pollard-p-1 rounds." fixnum)
31 (defmvar $pollard_rho_limit_step 1000 "Step for pollard-rho factorization limit." fixnum)
32 (defmvar $pollard_pm1_limit_step 5000 "Step for pollard-p-1 factorization limit." fixnum)
34 (defmvar $ecm_number_of_curves 50 "Number of curves tried in one round of ecm." fixnum)
35 (defmvar $ecm_limit 200 "Starting smootheness limit for ecm method." fixnum)
36 (defmvar $ecm_max_limit 51199 "Maximum smootheness for ecm method." fixnum)
37 (defmvar $ecm_limit_delta 200 "Increase smoothness limit for ecm method after each round." fixnum)
39 (defmvar $ifactor_verbose nil "Display factorization steps." boolean)
40 (defmvar $factors_only nil "Return a list of factors only." boolean)
42 (defun number-of-digits (n)
43 (length (format nil "~d" n)))
45 ;;; List of primes up to *largest-small-prime*
47 (defvar *largest-small-prime* 9973)
49 (defvar *small-primes*
50 '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
51 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181
52 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277
53 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383
54 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487
55 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601
56 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709
57 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
58 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947
59 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049
60 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151
61 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249
62 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361
63 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459
64 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559
65 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657
66 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759
67 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877
68 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997
69 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089
70 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213
71 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311
72 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411
73 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543
74 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663
75 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741
76 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851
77 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969
78 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089
79 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221
80 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331
81 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461
82 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557
83 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671
84 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779
85 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907
86 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013
87 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129
88 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243
89 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363
90 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493
91 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621
92 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729
93 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871
94 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973
95 4987 4993 4999 5003 5009 5011 5021 5023 5039 5051 5059 5077 5081 5087
96 5099 5101 5107 5113 5119 5147 5153 5167 5171 5179 5189 5197 5209 5227
97 5231 5233 5237 5261 5273 5279 5281 5297 5303 5309 5323 5333 5347 5351
98 5381 5387 5393 5399 5407 5413 5417 5419 5431 5437 5441 5443 5449 5471
99 5477 5479 5483 5501 5503 5507 5519 5521 5527 5531 5557 5563 5569 5573
100 5581 5591 5623 5639 5641 5647 5651 5653 5657 5659 5669 5683 5689 5693
101 5701 5711 5717 5737 5741 5743 5749 5779 5783 5791 5801 5807 5813 5821
102 5827 5839 5843 5849 5851 5857 5861 5867 5869 5879 5881 5897 5903 5923
103 5927 5939 5953 5981 5987 6007 6011 6029 6037 6043 6047 6053 6067 6073
104 6079 6089 6091 6101 6113 6121 6131 6133 6143 6151 6163 6173 6197 6199
105 6203 6211 6217 6221 6229 6247 6257 6263 6269 6271 6277 6287 6299 6301
106 6311 6317 6323 6329 6337 6343 6353 6359 6361 6367 6373 6379 6389 6397
107 6421 6427 6449 6451 6469 6473 6481 6491 6521 6529 6547 6551 6553 6563
108 6569 6571 6577 6581 6599 6607 6619 6637 6653 6659 6661 6673 6679 6689
109 6691 6701 6703 6709 6719 6733 6737 6761 6763 6779 6781 6791 6793 6803
110 6823 6827 6829 6833 6841 6857 6863 6869 6871 6883 6899 6907 6911 6917
111 6947 6949 6959 6961 6967 6971 6977 6983 6991 6997 7001 7013 7019 7027
112 7039 7043 7057 7069 7079 7103 7109 7121 7127 7129 7151 7159 7177 7187
113 7193 7207 7211 7213 7219 7229 7237 7243 7247 7253 7283 7297 7307 7309
114 7321 7331 7333 7349 7351 7369 7393 7411 7417 7433 7451 7457 7459 7477
115 7481 7487 7489 7499 7507 7517 7523 7529 7537 7541 7547 7549 7559 7561
116 7573 7577 7583 7589 7591 7603 7607 7621 7639 7643 7649 7669 7673 7681
117 7687 7691 7699 7703 7717 7723 7727 7741 7753 7757 7759 7789 7793 7817
118 7823 7829 7841 7853 7867 7873 7877 7879 7883 7901 7907 7919 7927 7933
119 7937 7949 7951 7963 7993 8009 8011 8017 8039 8053 8059 8069 8081 8087
120 8089 8093 8101 8111 8117 8123 8147 8161 8167 8171 8179 8191 8209 8219
121 8221 8231 8233 8237 8243 8263 8269 8273 8287 8291 8293 8297 8311 8317
122 8329 8353 8363 8369 8377 8387 8389 8419 8423 8429 8431 8443 8447 8461
123 8467 8501 8513 8521 8527 8537 8539 8543 8563 8573 8581 8597 8599 8609
124 8623 8627 8629 8641 8647 8663 8669 8677 8681 8689 8693 8699 8707 8713
125 8719 8731 8737 8741 8747 8753 8761 8779 8783 8803 8807 8819 8821 8831
126 8837 8839 8849 8861 8863 8867 8887 8893 8923 8929 8933 8941 8951 8963
127 8969 8971 8999 9001 9007 9011 9013 9029 9041 9043 9049 9059 9067 9091
128 9103 9109 9127 9133 9137 9151 9157 9161 9173 9181 9187 9199 9203 9209
129 9221 9227 9239 9241 9257 9277 9281 9283 9293 9311 9319 9323 9337 9341
130 9343 9349 9371 9377 9391 9397 9403 9413 9419 9421 9431 9433 9437 9439
131 9461 9463 9467 9473 9479 9491 9497 9511 9521 9533 9539 9547 9551 9587
132 9601 9613 9619 9623 9629 9631 9643 9649 9661 9677 9679 9689 9697 9719
133 9721 9733 9739 9743 9749 9767 9769 9781 9787 9791 9803 9811 9817 9829
134 9833 9839 9851 9857 9859 9871 9883 9887 9901 9907 9923 9929 9931 9941
135 9949 9967 9973))
137 ;;; List of numbers which have already been tested and are
138 ;;; primes > *largest-small-prime* (only used if $save_primes is true!).
140 (defvar *large-primes* ())
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;;; ;;;
144 ;;; ~*~ IMPLEMENTATION OF FACTORIZATION METHODS ~*~ ;;;
145 ;;; ;;;
146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
148 (defun get-factor-list (n)
149 (when $ifactor_verbose
150 (format t "~%Starting factorization of n = ~d~%" n))
151 (init-prime-diffs 640000)
152 (multiple-value-bind (large-part factor-list) (get-small-factors n)
153 (if (> large-part 1)
154 (append (convert-list (get-large-factors large-part)) factor-list)
155 factor-list)))
157 (defmfun $ifactors (n)
158 (unless (and (or (integerp n)
159 (and ($integerp n)
160 (setq n ($fix n)) ))
161 (plusp n) )
162 (merror (intl:gettext "ifactors: argument must be a positive integer; found: ~M") n))
163 (let* (($intfaclim)
164 (factor-list (get-factor-list n))
165 (factor-list (if $factors_only
166 (mapcar #'car factor-list)
167 (mapcar #'(lambda (u) `((mlist simp) ,(car u) ,(cadr u))) factor-list))))
168 ($sort `((mlist simp) ,@factor-list))))
170 ;; cfactor is the function used by maxima to factor integers
171 ;; (used form outside ifactor package)
173 (defun cfactor (x)
174 (cond ((null $factorflag) (return-from cfactor (list x 1)))
175 ((floatp x) (rat-error "`factor' given floating arg"))
176 ((pzerop x) (return-from cfactor (list (pzero) 1)))
177 ((equal x -1) (return-from cfactor (list -1 1)))
178 ((minusp x) (return-from cfactor (cons -1 (cons 1 (cfactor (- x))))))
179 ((< x 2) (return-from cfactor (list x 1)))
180 (t (let* ((factor-list (get-factor-list x))
181 (factor-list (sort factor-list #'< :key #'car))
182 (ans ()))
183 (dolist (fac factor-list ans)
184 (setq ans (cons (car fac) (cons (cadr fac) ans))))))))
186 ;;; we need to keep a list of differences between consecutive primes
187 ;;; for trial division, for stage 2 of ecm and the big prime variation of Pollard p-1
189 (defvar *prime-diffs* (make-array 100000 :element-type 'fixnum :adjustable t :initial-element 0)
190 "array of differences between consecutive primes")
192 (defvar *prime-diffs-limit* 1
193 "biggest prime in *prime-diffs")
195 (defvar *prime-diffs-maxindex* 1
196 "index of biggest valid prime difference")
198 (defvar *prime-diffs-maxdiff* 2
199 "maximum difference between consecutive primes in *prime-diffs*")
201 ;;; factor out primes < *prime-diffs-limit* by trial division
202 ;;; the array gets filled by a call to init-prime-diffs in get-factor-list
204 (defun get-small-factors (n)
205 (when (= n 1)
206 (return-from get-small-factors (values 1 nil)))
207 (when (< n 4) ;n = 2 or 3
208 (return-from get-small-factors (values 1 `((,n 1)))))
209 (let (factors)
210 ;; first divide off the even part
211 (loop with deg = 0
212 while (and (> n 1) (evenp n)) do
213 (setq n (ash n -1)) ; divide n by 2
214 (incf deg) ; and increment the exponent
215 finally
216 (when (plusp deg)
217 (push `(2 ,deg) factors)
218 (when $ifactor_verbose (format t "Factoring out 2: 2 (degree:~A)~%" deg))))
219 (when (= n 1)
220 (return-from get-small-factors (values 1 factors))) ; n was a power of 2
221 ;; now use the *prime-diffs* array for trial-factoring
222 (loop for i from 0 to *prime-diffs-maxindex*
223 and d = 3 then (+ d (aref *prime-diffs* i))
225 (when (> (* d d) n)
226 ;;(push `(,n 1) factors) replaced by workaround next line,
227 (push (list n 1) factors) ;; see bug report 3510983 (van_nek, 2012-03-27)
228 (when $ifactor_verbose (format t "small prime cofactor: ~A~%" n))
229 (return-from get-small-factors (values 1 factors)))
230 (loop with deg = 0
231 while (and (> n 1) (zerop (mod n d))) do
232 (setq n (truncate n d))
233 (incf deg)
234 finally
235 (when (plusp deg)
236 (push `(,d ,deg) factors)
237 (when $ifactor_verbose (format t "Factoring out small prime: ~A (degree:~A)~%" d deg))))
238 (when (= n 1)
239 (return-from get-small-factors (values 1 factors))))
240 (return-from get-small-factors (values n factors))))
242 ;;; get-large-factors returns the list of factors of integer n (n has
243 ;;; no small factor at this tage)
245 (defun get-large-factors (n)
246 (if (primep n)
247 (progn
248 (when $ifactor_verbose (format t "========> Prime factor: ~d~%~%" n))
249 (list n))
250 (get-large-factors-1 n)))
252 (defun get-large-factors-1 (n)
253 (let ((f (get-one-factor n)))
254 (if (= f n)
255 (progn
256 (when $ifactor_verbose (format t "Warning: could not find factors of composite:~%~A~%" n))
257 (list n))
258 (append (get-large-factors f) (get-large-factors (/ n f))))))
260 (defun get-one-factor (n)
261 (when $ifactor_verbose
262 (format t "Factoring n = ~d~%" n))
263 (let ((f nil)
264 (lim_pollard $pollard_rho_limit)
265 (lim_p-1 $pollard_pm1_limit)
266 ($ecm_number_of_curves $ecm_number_of_curves))
268 ;; If $intfaclim is not false then we don't want to spend too much
269 ;; time factoring integers so we return n and leave it
270 ;; unfactored. The default value for $intfaclim is true, but most
271 ;; functions which use integer factorization set it to false.
272 (when $intfaclim
273 (return-from get-one-factor n))
275 ;; first try known large primes
276 (dolist (p *large-primes*)
277 (when (zerop (mod n p))
278 (return-from get-one-factor p)))
280 ;; try factoring smaller factors with pollard-rho
281 (dotimes (i $pollard_rho_tests)
282 (when $ifactor_verbose
283 (format t "Pollard rho: round #~d of ~d (lim=~d)~%" (1+ i) $pollard_rho_tests lim_pollard))
284 (setq f (get-one-factor-pollard n lim_pollard))
285 (when (< 1 f n)
286 (when $ifactor_verbose
287 (format t "Pollard rho: found factor ~A (~d digits)~%" f (number-of-digits f)))
288 (return-from get-one-factor f))
289 (if (> lim_pollard 0)
290 (incf lim_pollard $pollard_rho_limit_step)))
292 ;; now try factoring with pollards p-1 method
293 (dotimes (i $pollard_pm1_tests)
294 (when $ifactor_verbose
295 (format t "Pollard p-1: round #~d of ~d (lim=~d)~%" (1+ i) $pollard_pm1_tests lim_p-1))
296 (setq f (get-one-factor-p-1 n lim_p-1))
297 (when (< 1 f n)
298 (when $ifactor_verbose
299 (format t "Pollard p-1: found factor ~A (~d digits)~%" f (number-of-digits f)))
300 (return-from get-one-factor f))
301 (when (plusp lim_pollard)
302 (incf lim_p-1 $pollard_pm1_limit_step)))
304 ;; continue with ecm
305 (do ()
306 (nil)
307 (setq f (get-one-factor-ecm n))
308 (unless (null f)
309 (return-from get-one-factor f))
310 (setq $ecm_number_of_curves (+ $ecm_number_of_curves 50)))))
312 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 ;;; ;;;
314 ;;; ~*~ IMPLEMENTATION OF POLLARDS-RHO FACTORIZATION METHOD ~*~ ;;;
315 ;;; ;;;
316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
318 ;; This is the Brent's variant of Pollard's rho method.
320 (defun get-one-factor-pollard (n lim)
321 (let* ((x (+ (random (- n 3)) 2))
322 (a (+ (random (- n 2)) 1))
323 (b (+ (random (- n 5)) 1))
324 (y x) (d 1) (r 2) (j 1) (k)
325 (terms (integer-length n)) )
326 (setq b (/ b (gcd a b)))
327 (loop while (= d 1) do
328 (setq y x)
329 (incf j r)
330 (dotimes (i r)
331 (setq x (mod (+ (* a (mod (* x x) n)) b) n)))
332 (setq k 0)
333 (loop while (and (< k r) (equal d 1)) do
334 (dotimes (i (min terms (- r k)))
335 (setq x (mod (+ (* a (mod (* x x) n)) b) n))
336 (setq d (mod (* d (- x y)) n)))
337 (setq d (gcd d n))
338 (incf k terms))
339 (setq r (* 2 r))
340 (when (< 0 lim j)
341 (return-from get-one-factor-pollard d)))
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
345 ;;; ;;;
346 ;;; ~*~ IMPLEMENTATION OF POLLARDS P-1 FACTORIZATION METHOD ~*~ ;;;
347 ;;; ;;;
348 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 ;;; product of all primes low < p < high and all integers
351 ;;; isqrt(low) < n < isqrt(high)
353 (defun ppexpo (low high)
354 (declare (integer low high))
355 (let ((x 1))
356 (loop for i from (max 2 (1+ (isqrt low))) to (isqrt high) do
357 (setq x (* i x)))
358 (when (oddp low) (incf low))
359 (loop for i from (1+ low) to high
360 when (primep i) do (setq x (* i x)))
363 (defun big-prime-var (y n bound)
364 (let ((x (make-array (1+ (ash *prime-diffs-maxdiff* -1)) :element-type 'integer :initial-element 1))
365 (y2 (mod (* y y) n))
366 (q 3)
367 (count 1)
368 (d 0)
369 (z 0)
370 (k 0))
371 (loop for i from 1 to (ash *prime-diffs-maxdiff* -1) do
372 (setf (aref x i) (mod (* y2 (aref x (1- i))) n)))
373 (setq y (power-mod y q n))
374 (setq z (1- y))
375 (setq bound (min bound *prime-diffs-limit*))
376 (loop for i from 0
377 while (< q bound) do
378 (setq k (aref *prime-diffs* i))
379 (incf q k)
380 (setq y (mod (* y (aref x (ash k -1))) n))
381 (setq z (mod (* z (1- y)) n))
382 (when (> (incf count) 1000)
383 (setq count 0)
384 (setq d (gcd z n))
385 (when (> d 1)
386 (return-from big-prime-var d))))
389 ;;; Pollard's p-1 factoring algorithm
390 ;;; in general a prime factor p of x is found, if p-1 is
391 ;;; a product of prime powers q^k <= lim
393 (defun get-one-factor-p-1 (n &optional (lim 16000))
394 (declare (integer n lim))
395 (let* ((base (+ 2 (random (- n 2))))
396 (anz 256)
397 (d (gcd base n)))
398 (declare (fixnum anz)
399 (integer base d))
400 (when (< 1 d n) (return-from get-one-factor-p-1 d))
401 (loop for n0 from 0 to (1- lim) by anz
402 and ex = (ppexpo n0 (min lim (+ n0 anz))) do
403 (setq base (power-mod base ex n))
404 (when (<= base 1) (return-from get-one-factor-p-1 1))
405 (setq d (gcd (1- base) n))
406 (when (> d 1)
407 (return-from get-one-factor-p-1 d)))
408 (when (= d 1)
409 (return-from get-one-factor-p-1 (big-prime-var base n *prime-diffs-limit*))))
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 ;;; ;;;
414 ;;; ~*~ IMPLEMENTATION OF ELLIPTIC CURVE FACTORIZATION METHOD ~*~ ;;;
415 ;;; ;;;
416 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
418 ;;; Elliptic curve factorization method as described in the paper
419 ;;; R. Brent: "Factorization of the tenth and eleventh Fermat Number".
420 ;;; The paper is in file rpb161tr.dvi.gz from
421 ;;; ftp://ftp.comlab.ox.ac.uk/pub/Documents/techpapers/Richard.Brent/
423 ;;; Based in part on the implementation from GAP4 FacInt package.
425 (defun init-prime-diffs (n)
426 (when (> n *prime-diffs-limit*)
427 (setq n (* 2 n))
428 (when $ifactor_verbose (format t "Initializing prime diffs up to n=~d~%" n))
429 (let ((sieve (make-array (1+ n) :element-type 'bit :initial-element 1)))
430 (do ((p 3 ($next_prime p)))
431 ((> p (isqrt n)))
432 (do ((d (* 2 p) (+ d p)))
433 ((> d n))
434 (setf (sbit sieve d) 0)))
435 (do ((q1 3)
436 (i 0)
437 (q2 5 (+ q2 2)))
438 ((> q2 n) (setq *prime-diffs-maxindex* (1- i)))
439 (when (= 1 (sbit sieve q2))
440 (when (>= i (length *prime-diffs*))
441 (setq *prime-diffs* (adjust-array *prime-diffs* (* 2 (length *prime-diffs*)) :element-type 'fixnum :initial-element 0))
442 (when $ifactor_verbose
443 (format t "init-prime-diffs: adjusting *prime-diffs* to size ~d~%" (* 2 (length *prime-diffs*)))))
444 (setq *prime-diffs-limit* q2)
446 (let ((diff (- q2 q1)))
447 (setf (aref *prime-diffs* i) diff)
448 (when (> diff *prime-diffs-maxdiff*) (setq *prime-diffs-maxdiff* diff)))
449 (setq q1 q2)
450 (incf i))))))
452 ;;; modular inverse of a (modulus m)
454 ;;; this implementation of the modular inverse is different to `invmod' in src/rat3a.lisp
455 ;;; inv-mod returns a positive modulo or `nil' in case of a zero divisor
457 (defun inv-mod (a m)
458 (let ((u1 0)(u2 m)(v1 1)(v2 (mod a m)) q r)
459 (do ()((zerop v2)
460 (if (= 1 u2) (mod u1 m) nil) )
461 (multiple-value-setq (q r) (truncate u2 v2))
462 (psetq u1 v1 v1 (- u1 (* q v1)))
463 (setq u2 v2 v2 r) )))
465 (defmfun $inv_mod (a m)
466 (unless (and (integerp a) (integerp m))
467 (merror (intl:gettext "inv_mod: arguments must be integers; found: ~M, ~M") a m))
468 (unless (= 0 a) (inv-mod a m)) )
470 ;;; computations on the elliptic curve:
471 ;;; we use the elliptic curve in projective coordinates (x,y,z), but only
472 ;;; use x and z
474 (defun ecm-product (q p1 p2 n)
475 (let ((x1 (car p1)) (x2 (car p2))
476 (z1 (cadr p1)) (z2 (cadr p2))
477 (pr1) (pr2) (sq1) (sq2) (x3) (z3))
478 (setq pr1 (mod (* (- x1 z1) (+ x2 z2)) n))
479 (setq pr2 (mod (* (+ x1 z1) (- x2 z2)) n))
480 (setq sq1 (mod (expt (+ pr1 pr2) 2) n))
481 (setq sq2 (mod (expt (- pr1 pr2) 2) n))
482 (setq x3 (mod (* (cadr q) sq1) n))
483 (setq z3 (mod (* (car q) sq2) n))
484 `(,x3 ,z3)))
486 (defun ecm-square (p n a)
487 (let ((x1 (car p)) (z1 (cadr p))
488 (x2) (z2) (sq1) (sq2) (f1) (f2))
489 (setq sq1 (mod (* (+ x1 z1) (+ x1 z1)) n))
490 (setq sq2 (mod (* (- x1 z1) (- x1 z1)) n))
491 (setq f1 (- sq1 sq2))
492 (setq f2 (mod (* a f1) n))
493 (setq x2 (mod (* sq1 sq2) n))
494 (setq z2 (mod (* f1 (+ sq2 f2)) n))
495 `(,x2 ,z2)))
497 (defun ecm-power (base e n a)
498 (let ((p base) (ptb (ecm-square base n a)) (l (integer-length e)))
499 (do ((i (- l 2) (1- i)))
500 ((< i 0))
501 (if (logbitp i e)
502 (progn
503 (setq p (ecm-product base p ptb n))
504 (setq ptb (ecm-square ptb n a)))
505 (progn
506 (setq ptb (ecm-product base p ptb n))
507 (setq p (ecm-square p n a)))))
510 (defun ecm-factor-with-curve (n x z a lim1)
511 (let ((g (gcd (- (* a a) 4) n)))
512 (unless (= g 1) (return-from ecm-factor-with-curve g)))
513 (setq a (mod (floor (+ a 2) 4) n))
515 ;; stage 1: compute p^M where M=p1^e1*...*pk^ek where
516 ;; p1,...,pk are primes < lim1 and ei=log[pi](n)
518 (let ((q 1)
519 (last_q ($prev_prime lim1))
520 (p `(,x ,z))
521 (ex) (next_gcd) (gcd_interval))
522 (setq gcd_interval (floor lim1 4))
523 (setq next_gcd gcd_interval)
524 (do ()
525 ((> q lim1))
526 (setq q ($next_prime q))
527 (setq ex (floor (log lim1) (log q)))
528 (cond ((= q 2) (incf ex 2))
529 ((= q 3) (incf ex)))
530 (setq p (ecm-power p (expt q ex) n a))
531 (when (>= q next_gcd)
532 (let ((g (gcd (cadr p) n)))
533 (when (< 1 g n)
534 (when $ifactor_verbose
535 (format t "ECM: found factor in stage 1: ~d (~d digits)~%" g (number-of-digits g)))
536 (return-from ecm-factor-with-curve g))
537 (setq next_gcd (min (+ next_gcd gcd_interval) last_q)))))
539 ;; stage 2: compute (p^M)^pi for each prime lim1<pi<lim2 (and some
540 ;; other exponents)
541 ;; Uses "Improved standard cotinuation".
543 (let* ((lim2 (* lim1 100))
544 (power-after-1 p)
545 (step-size (min (/ lim1 2) (isqrt (/ lim2 2))))
546 (d-step-size (* 2 step-size))
547 (power-table (make-array (+ 2 step-size)))
548 (d-step-size-power (ecm-power power-after-1 d-step-size n a))
549 (step-power (ecm-power power-after-1 (1+ d-step-size) n a))
550 (last-step-power power-after-1)
551 (step-pos 1)
552 (q1 3)
553 (prime-diffs-pos 0)
554 (step-power-buff))
555 (init-prime-diffs lim2)
556 (setf (aref power-table 1) (ecm-square power-after-1 n a))
557 (setf (aref power-table 2) (ecm-square (aref power-table 1) n a))
558 (do ((i 3 (1+ i)))
559 ((> i step-size))
560 (setf (aref power-table i)
561 (ecm-product (aref power-table (- i 2)) (aref power-table 1) (aref power-table (1- i)) n)))
562 (do ()
563 ((> step-pos (- lim2 d-step-size)))
564 (let ((buff-prod 1)
565 (q-limit (+ step-pos d-step-size))
566 (power-table-pos (/ (- q1 step-pos) 2)))
567 (when (zerop power-table-pos) ($error q1 step-pos))
568 (do ()
569 ((> q1 q-limit))
570 (let* ((sp1 (car step-power))
571 (sp2 (cadr step-power))
572 (pp1 (car (aref power-table power-table-pos)))
573 (pp2 (cadr (aref power-table power-table-pos)))
574 (coord-diffs (mod (- (* sp1 pp2) (* sp2 pp1)) n)))
575 (setq buff-prod (mod (* coord-diffs buff-prod) n)))
576 (incf q1 (aref *prime-diffs* prime-diffs-pos))
577 (incf power-table-pos (/ (aref *prime-diffs* prime-diffs-pos) 2))
578 (incf prime-diffs-pos))
580 (let ((g (gcd n buff-prod)))
581 (when (> g 1)
582 (when $ifactor_verbose
583 (format t "ECM: found factor in stage 2: ~d (~d digits)~%" g (number-of-digits g)))
584 (return-from ecm-factor-with-curve g)))
586 (setq step-power-buff step-power)
587 (setq step-power (ecm-product last-step-power d-step-size-power step-power n))
588 (setq last-step-power step-power-buff)
589 (incf step-pos d-step-size))))
590 nil))
592 (defun get-one-factor-ecm (n)
593 (when (primep n) (return-from get-one-factor-ecm n))
594 (let ((sigma (+ 6 (random (ash 1 20))))
595 (x 0) (z 0) (u 0) (v 0) (a 0) (a1 0) (a2 0)
596 (fact) (lim1 $ecm_limit) (a2_inv 0))
597 (dotimes (i $ecm_number_of_curves)
598 (setq u (mod (- (* sigma sigma) 5) n))
599 (setq v (mod (* 4 sigma) n))
600 (setq x (mod (expt u 3) n))
601 (setq z (mod (expt v 3) n))
602 (setq a1 (mod (* (expt (- v u) 3) (+ (* 3 u) v)) n))
603 (setq a2 (mod (* 4 x v) n))
604 (setq a2_inv (inv-mod a2 n))
605 (when (null a2_inv)
606 (return-from get-one-factor-ecm (gcd a2 n)))
607 (setq a (mod (* a1 a2_inv) n))
608 (setq sigma (max 6 (mod (+ (* sigma sigma) 1) n)))
609 (when $ifactor_verbose
610 (format t "ECM: trying with curve #~d of ~d (lim=~d)~%" (1+ i) $ecm_number_of_curves lim1))
611 (setq fact (ecm-factor-with-curve n x z a lim1))
612 (when (and fact (< fact n))
613 (return-from get-one-factor-ecm fact))
614 (setq lim1 (min (+ lim1 $ecm_limit_delta) $ecm_max_limit)))
615 nil))
618 ;;; convert (3 5 3 5 3 7) to ((3 3) (5 2) (7 1))
620 (defun convert-list (l)
621 (labels ((convert-list-sub (e n l1 l2)
622 (cond ((null l1)
623 (cons (list e n) l2))
624 ((= e (car l1))
625 (convert-list-sub e (1+ n) (cdr l1) l2))
626 (t (convert-list-sub (car l1) 1 (cdr l1) (cons `(,e ,n) l2))))))
627 (let ((l1 (sort l #'>)))
628 (convert-list-sub (car l1) 1 (rest l1) nil))))
630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631 ;;; ;;;
632 ;;; ~*~ IMPLEMENTATION OF PRIMALITY TESTS ~*~ ;;;
633 ;;; ;;;
634 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
636 (defmfun $primep (n)
637 (if (integerp n)
638 (primep (abs n))
639 (merror (intl:gettext "primep: argument must be an integer; found: ~M") n)))
641 (defun primep (n)
642 (cond
643 ((= n 1) nil)
644 ((evenp n) (= n 2))
645 ((<= n *largest-small-prime*) (when (member n *small-primes*) t))
646 ((< n 9080191) (primep-small n '(31 73)))
647 ((< n 4759123141) (primep-small n '(2 7 61)))
648 ((< n 2152302898747) (primep-small n '(2 3 5 7 11)))
649 ((< n 3474749660383) (primep-small n '(2 3 5 7 11 13)))
650 ((< n 341550071728321) (primep-small n '(2 3 5 7 11 13 17)))
651 ((< n 3825123056546413051) (primep-small n '(2 3 5 7 11 13 17 19 23)))
652 ((< n 318665857834031151167461) (primep-small n '(2 3 5 7 11 13 17 19 23 29 31 37)))
653 ((< n 3317044064679887385961981) (primep-small n '(2 3 5 7 11 13 17 19 23 29 31 37 41)))
654 ((member n *large-primes*) t)
655 (t (primep-prob n)) ))
657 ;;; A Miller-Rabin test is deterministic for small n if we test for small bases.
658 ;;; Reference:
659 ;;; [1] G. Jaeschke, On Strong Pseudoprimes to Several Bases,
660 ;;; Math. Comp., 61 (1993), 915-926.
661 ;;; [2] http://primes.utm.edu/prove/prove2_3.html
662 ;;; [3] Jiang, Deng - Strong pseudoprimes to the first eight prime bases (2014)
663 ;;; Mathematics of Computation, Vol 83, Nr 290, Pages 2915--2924
664 ;;; [3] Sorenson, Webster - Strong Pseudoprimes to Twelve Prime Bases (2015)
665 ;;; arXiv:1509.00864v1 [math.NT]
667 (defun primep-small (n bases)
668 (multiple-value-bind (q k) (miller-rabin-decomposition n)
669 (dolist (x bases t)
670 (unless (miller-rabin-kernel n q k x)
671 (return-from primep-small nil) ))))
673 ;;; strong primality test:
674 ;;; - run $primep_number_of_tests times a Miller-Rabin test
675 ;;; - run one Lucas test
677 (defun primep-prob (n)
678 ;; Miller-Rabin tests:
679 (multiple-value-bind (q k) (miller-rabin-decomposition n)
680 (dotimes (i $primep_number_of_tests)
681 (unless (miller-rabin-kernel n q k)
682 (return-from primep-prob nil) )))
683 ;; Lucas test:
684 (primep-lucas n) )
687 ;;; Miller-Rabin (algorithm P from D. Knuth, TAOCP, 4.5.4)
689 ;;; - write n-1 = q*2^k (n,q odd, n > 2)
690 ;;; - x is a random number 1 < x < n
691 ;;; - n passes the test if
692 ;;; x^q = 1 (mod n)
693 ;;; or x^(q*2^j) = -1 (mod n) for some j = 0..k-1
695 ;;; A prime number must pass this test.
696 ;;; The probability of passing one test and not being a prime is less than 1/4.
698 ;; return values q,k with n-1 = q*2^k
699 (defun miller-rabin-decomposition (n) ;; assume n > 2 (n-1 is even)
700 (do ((k 1 (1+ k))
701 (q (ash n -1) (ash q -1)) )
702 ((logbitp 0 q) (values q k)) ))
704 ;; now assume n-1 = q*2^k, k >= 1
705 (defun miller-rabin-kernel (n q k &optional x)
706 (unless x
707 (setq x (+ (random (- n 2)) 2)) )
708 (let ((y (power-mod x q n)) ;; j = 0
709 (minus1 (1- n)) )
710 (if (or (= y 1) (= y minus1))
712 (do ((j 1 (1+ j)))
713 ((= j k))
714 (setq y (power-mod y 2 n))
715 (when (= y minus1) (return t))
716 (when (= y 1) (return)) )))) ;; n prime => last y must have been 1 or -1
719 (defmfun $power_mod (b e m)
720 (unless (and (integerp b) (integerp e) (integerp m))
721 (merror (intl:gettext "power_mod: arguments must be integers; found: ~M, ~M, ~M") b e m) )
722 (if (>= e 0)
723 (power-mod b e m)
724 (let ((inv (inv-mod b m)))
725 (when inv
726 (power-mod inv (- e) m) ))))
728 (defun power-mod (b e m)
729 (declare (optimize (speed 3) (safety 0)))
730 (cond
731 ((zerop e)
732 (mod 1 m) )
733 ((typep e 'fixnum)
734 (do ((res 1)) (())
735 (when (logbitp 0 e)
736 (setq res (mod (* res b) m))
737 (when (= 1 e) (return res)) )
738 (setq e (ash e -1)
739 b (mod (* b b) m)) ))
740 (t ;; sliding window variant:
741 (let* ((l (integer-length e))
742 (k (cond ((< l 65) 3)
743 ((< l 161) 4)
744 ((< l 385) 5)
745 ((< l 897) 6)
746 (t 7) ))
747 (tab (power-mod-tab b k m))
748 (res 1) s u tmp )
749 (do ((i (1- l)))
750 ((< i 0) res)
751 (cond
752 ((logbitp i e)
753 (setq s (max (1+ (- i k)) 0))
754 (do () ((logbitp s e)) (incf s))
755 (setq tmp (1+ (- i s)))
756 (dotimes (h tmp) (setq res (mod (* res res) m)))
757 (setq u (ldb (byte tmp s) e))
758 (unless (= u 0) (setq res (mod (* res (svref tab (ash u -1))) m)))
759 (setq i (1- s)) )
761 (setq res (mod (* res res) m))
762 (decf i) )))))))
764 (defun power-mod-tab (b k m)
765 (declare (optimize (speed 3) (safety 0)))
766 (let* ((l (ash 1 (1- k)))
767 (tab (make-array l :element-type 'integer :initial-element 1))
768 (bi b)
769 (bb (mod (* b b) m)) )
770 (setf (svref tab 0) b)
771 (do ((i 1 (1+ i)))
772 ((= i l) tab)
773 (setq bi (mod (* bi bb) m))
774 (setf (svref tab i) bi) )))
776 ;;; primep-lucas:
778 ;;; Define: x^2-a*x+b, D=a^2-4*b; x1, x2 roots of x^2+a*x+b;
779 ;;; U[k]=(x1^k-x2^k)/(x1-x2), V[k]=x1^k+x2^k.
781 ;;; Lucas theorem: If p is an odd prime, gcd(p,b)=1 and jacobi(D,p)=-1,
782 ;;; then p divides U[p+1].
784 ;;; We calculate U[p+1] for x^2-b*x+1 where jacobi(b^2-4,n)=-1
785 ;;; and test if p divides U[p+1].
787 (defun primep-lucas (n)
788 (let (prmp (b 3))
789 (loop while (not (= ($jacobi (- (* b b) 4) n) -1)) do
790 (incf b))
791 (setq prmp (zerop (lucas-sequence (1+ n) b n)))
792 (when (and prmp $save_primes)
793 (push n *large-primes*))
794 prmp))
796 ;;; Get element U[p+1] of Lucas sequence for x^2-p*x+1.
798 ;;; Uses algorithm from M. Joye and J.-J. Quisquater,
799 ;;; Efficient computation of full Lucas sequences, 1996
801 (defun lucas-sequence (k p n)
802 (let ((uh 1) (vl 2) (vh p) (s 0) l)
803 (do ()
804 ((logbitp 0 k))
805 (setq k (ash k -1))
806 (setq s (1+ s)))
808 (setq l (integer-length k))
810 (do ((j (1- l) (1- j)))
811 ((= 0 j))
812 (if (logbitp j k)
813 (progn
814 (setq uh (mod (* uh vh) n))
815 (setq vl (mod (- (* vh vl) p) n))
816 (setq vh (mod (- (* vh vh) 2) n)))
817 (progn
818 (setq uh (mod (1- (* uh vl)) n))
819 (setq vh (mod (- (* vh vl) p) n))
820 (setq vl (mod (- (* vl vl) 2) n)))))
822 (setq uh (mod (1- (* uh vl)) n))
823 (setq vl (mod (- (* vh vl) p) n))
825 (dotimes (j s)
826 (setq uh (mod (* uh vl) n))
827 (setq vl (mod (- (* vl vl) 2) n)))
828 uh))
830 ;;; first values of next_prime
831 (defvar *next_prime_ar* #(0 2 3 5 5 7 7))
833 ;;; first values of prev_prime
834 (defvar *prev_prime_ar* #(0 0 0 2 3 3 5 5 7 7 7 7))
836 ;;; gaps between numbers that are not multiples of 2,3,5,7
837 (defvar deltaprimes_next
838 '(1 10 9 8 7 6 5 4 3 2 1 2 1 4 3 2 1 2 1 4 3 2 1 6 5 4 3 2 1 2
839 1 6 5 4 3 2 1 4 3 2 1 2 1 4 3 2 1 6 5 4 3 2 1 6 5 4 3 2 1 2 1
840 6 5 4 3 2 1 4 3 2 1 2 1 6 5 4 3 2 1 4 3 2 1 6 5 4 3 2 1 8 7 6
841 5 4 3 2 1 4 3 2 1 2 1 4 3 2 1 2 1 4 3 2 1 8 7 6 5 4 3 2 1 6 5
842 4 3 2 1 4 3 2 1 6 5 4 3 2 1 2 1 4 3 2 1 6 5 4 3 2 1 2 1 6 5 4
843 3 2 1 6 5 4 3 2 1 4 3 2 1 2 1 4 3 2 1 6 5 4 3 2 1 2 1 6 5 4 3
844 2 1 4 3 2 1 2 1 4 3 2 1 2 1 10 9 8 7 6 5 4 3 2 1 2))
846 (defvar deltaprimes_prev
847 '(-1 -2 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -1 -2 -1 -2 -3 -4 -1 -2
848 -1 -2 -3 -4 -1 -2 -3 -4 -5 -6 -1 -2 -1 -2 -3 -4 -5 -6 -1 -2 -3
849 -4 -1 -2 -1 -2 -3 -4 -1 -2 -3 -4 -5 -6 -1 -2 -3 -4 -5 -6 -1 -2
850 -1 -2 -3 -4 -5 -6 -1 -2 -3 -4 -1 -2 -1 -2 -3 -4 -5 -6 -1 -2 -3
851 -4 -1 -2 -3 -4 -5 -6 -1 -2 -3 -4 -5 -6 -7 -8 -1 -2 -3 -4 -1 -2
852 -1 -2 -3 -4 -1 -2 -1 -2 -3 -4 -1 -2 -3 -4 -5 -6 -7 -8 -1 -2 -3
853 -4 -5 -6 -1 -2 -3 -4 -1 -2 -3 -4 -5 -6 -1 -2 -1 -2 -3 -4 -1 -2
854 -3 -4 -5 -6 -1 -2 -1 -2 -3 -4 -5 -6 -1 -2 -3 -4 -5 -6 -1 -2 -3
855 -4 -1 -2 -1 -2 -3 -4 -1 -2 -3 -4 -5 -6 -1 -2 -1 -2 -3 -4 -5 -6
856 -1 -2 -3 -4 -1 -2 -1 -2 -3 -4 -1 -2 -1 -2 -3 -4 -5 -6 -7 -8 -9
857 -10))
859 ;;; product of primes in [59..2897]
860 (defvar bigprimemultiple 6805598092615180737440235028147472981586738014295015027644884201753964648883910180850814465749532893719128055374719237806417537893593625321589379773764981786235326314555704406245399180879758341371676681401881451390195684863765326592983982964414393796690715805513465774520452671995927595391575142047776807977863591126244782181086547150369260177339043045082132788709080989495477932949788444703905327686499493503904132269141007955089790798876488207574072278769735865653223865994494346936718462923487228576140267887355548289736131557613540186975875834980017431190021254898173201223012171417763388931502928376549397638685218312217808199405294916194758171476025904777185780125034583816795375331627264462778001498062163759312245245590800878057927864359433868165604228946307536835897173733369926842890411102870160854438921809703357774373318146115616129588245083207631664167515206143659538759733110973189757163548882116479710800109577584318611988710048552969742803870964125788279451564113232340649434743105271873797620278073136369295820926294656549976175331880139356684249842712956493849288710258349886914201056170180503844749859595207139766052196982574437241716274871254310342540993006427120762049161745282399431514257565489)
862 (defmfun $next_prime (n)
863 (unless (and (integerp n))
864 (merror (intl:gettext "next_prime: argument must be an integer; found: ~M") n))
865 (cond ((< n 2) 2)
866 ((<= n 6) (aref *next_prime_ar* n))
867 ((< n 100000) (return-from $next_prime (next-prime-det n deltaprimes_next)))
868 (t (next-prime-prob n deltaprimes_next))))
870 (defmfun $prev_prime (n)
871 (unless (and (integerp n) (> n 2))
872 (merror (intl:gettext "prev_prime: argument must be an integer greater than 2; found: ~M") n))
873 (if (<= n 11) (return-from $prev_prime (aref *prev_prime_ar* n)))
874 (if (< n 100000) (return-from $prev_prime (next-prime-det n deltaprimes_prev)))
875 (next-prime-prob n deltaprimes_prev))
878 ;;; Find next/prev prime using deterministic test that checks all
879 ;;; divisors < sqrt(n) and skipping all multiples of 2,3,5,7
880 ;;; preconditions: 11 < n < 9973*9973
881 (defun next-prime-det (n deltaprimes)
882 (incf n (nth (mmod n 210) deltaprimes))
883 (loop while 1 do
884 (dolist (p *small-primes*)
885 (if (= (mmod n p) 0) (return))
886 (if (>= (* p p) n) (return-from next-prime-det n)))
887 (incf n (nth (mmod n 210) deltaprimes))))
889 ;;; Find next/prev prime using probabilistic test and skipping al multiples of
890 ;;; 2,3,5,7 using deltaprimes list and calculating gcd's with product of
891 ;;; prime numbers
892 (defun next-prime-prob (n deltaprimes)
893 ;; skip all multiples of 2,3,5,7
894 (incf n (nth (mmod n 210) deltaprimes))
895 (loop
896 (and
897 ;; gcd agaist product of primes in [11..31]
898 (= (gcd n 955049953) 1)
899 ;; gcd agaist product of primes in [37..53]
900 (= (gcd n 162490421) 1)
901 ;; gcd agaist product of primes in [59..2897]
902 (= (gcd n bigprimemultiple) 1)
903 (primep n)
904 (return-from next-prime-prob n))
905 ;; skip all multiples of 2,3,5,7"
906 (incf n (nth (mmod n 210) deltaprimes))))
909 (defun next-prime (n c)
910 (when (evenp n) (incf n c))
911 (loop
912 (when (primep n)
913 (return-from next-prime n))
914 (incf n (* 2 c))))
916 ;;; return a list of all primes between start and end
918 (defmfun $primes (start end)
919 (unless (and (integerp start) (integerp end))
920 (merror (intl:gettext "primes: arguments must be integers; found: ~M, ~M") start end))
921 (let ((primes nil))
922 (cond
923 ;; take primes from *small-primes* if possible
924 ((<= start *largest-small-prime*)
925 (dolist (n *small-primes*)
926 (when (<= start n end)
927 (push n primes) ))
928 (setq start *largest-small-prime*) )
930 (decf start) )) ; $next_prime returns a value >= argument + 1
931 ;; search for the rest of primes
932 (do ((n ($next_prime start) ($next_prime (1+ n))))
933 ((> n end) (cons '(mlist) (reverse primes)))
934 (push n primes) )))