1 ;;; calc-comb.el --- combinatoric functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defconst math-primes-table
36 [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
37 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181
38 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277
39 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383
40 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487
41 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601
42 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709
43 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
44 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947
45 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049
46 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151
47 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249
48 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361
49 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459
50 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559
51 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657
52 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759
53 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877
54 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997
55 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089
56 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213
57 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311
58 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411
59 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543
60 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663
61 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741
62 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851
63 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969
64 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089
65 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221
66 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331
67 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461
68 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557
69 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671
70 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779
71 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907
72 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013
73 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129
74 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243
75 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363
76 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493
77 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621
78 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729
79 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871
80 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973
83 ;; The variable math-prime-factors-finished is set by calcFunc-prfac to
84 ;; indicate whether factoring is complete, and used by calcFunc-factors,
85 ;; calcFunc-totient and calcFunc-moebius.
86 (defvar math-prime-factors-finished
)
93 (calc-binary-op "gcd" 'calcFunc-gcd arg
)))
98 (calc-binary-op "lcm" 'calcFunc-lcm arg
)))
100 (defun calc-extended-gcd ()
103 (calc-enter-result 2 "egcd" (cons 'calcFunc-egcd
(calc-top-list-n 2)))))
105 (defun calc-factorial (arg)
108 (calc-unary-op "fact" 'calcFunc-fact arg
)))
110 (defun calc-gamma (arg)
113 (calc-unary-op "gmma" 'calcFunc-gamma arg
)))
115 (defun calc-double-factorial (arg)
118 (calc-unary-op "dfac" 'calcFunc-dfact arg
)))
120 (defun calc-choose (arg)
123 (if (calc-is-hyperbolic)
124 (calc-binary-op "perm" 'calcFunc-perm arg
)
125 (calc-binary-op "chos" 'calcFunc-choose arg
))))
127 (defun calc-perm (arg)
129 (calc-hyperbolic-func)
132 (defvar calc-last-random-limit
'(float 1 0))
133 (defun calc-random (n)
137 (calc-enter-result 0 "rand" (list 'calcFunc-random
138 (calc-get-random-limit
139 (prefix-numeric-value n
))))
140 (calc-enter-result 1 "rand" (list 'calcFunc-random
141 (calc-get-random-limit
144 (defun calc-get-random-limit (val)
146 calc-last-random-limit
147 (setq calc-last-random-limit val
)))
149 (defun calc-rrandom ()
152 (setq calc-last-random-limit
'(float 1 0))
153 (calc-enter-result 0 "rand" (list 'calcFunc-random
'(float 1 0)))))
155 (defun calc-random-again (arg)
158 (while (>= (setq arg
(1- arg
)) 0)
159 (calc-enter-result 0 "rand" (list 'calcFunc-random
160 calc-last-random-limit
)))))
162 (defun calc-shuffle (n)
166 (calc-enter-result 1 "shuf" (list 'calcFunc-shuffle
167 (prefix-numeric-value n
)
168 (calc-get-random-limit
170 (calc-enter-result 2 "shuf" (list 'calcFunc-shuffle
172 (calc-get-random-limit
175 (defun calc-report-prime-test (res)
176 (cond ((eq (car res
) t
)
177 (calc-record-message "prim" "Prime (guaranteed)"))
180 (if (eq (nth 1 res
) 'unknown
)
182 "prim" "Non-prime (factors unknown)")
184 "prim" "Non-prime (%s is a factor)"
185 (math-format-number (nth 1 res
))))
186 (calc-record-message "prim" "Non-prime")))
189 "prim" "Probably prime (%d iters; %s%% chance of error)"
191 (let ((calc-float-format '(fix 2)))
192 (math-format-number (nth 2 res
)))))))
194 (defun calc-prime-test (iters)
197 (let* ((n (calc-top-n 1))
198 (res (math-prime-test n iters
)))
199 (calc-report-prime-test res
))))
201 (defvar calc-verbose-nextprime nil
)
203 (defun calc-next-prime (iters)
206 (let ((calc-verbose-nextprime t
))
207 (if (calc-is-inverse)
208 (calc-enter-result 1 "prvp" (list 'calcFunc-prevprime
209 (calc-top-n 1) (math-abs iters
)))
210 (calc-enter-result 1 "nxtp" (list 'calcFunc-nextprime
211 (calc-top-n 1) (math-abs iters
)))))))
213 (defun calc-prev-prime (iters)
216 (calc-next-prime iters
))
218 (defun calc-prime-factors (iters)
221 (let ((res (calcFunc-prfac (calc-top-n 1))))
222 (if (not math-prime-factors-finished
)
223 (calc-record-message "pfac" "Warning: May not be fully factored"))
224 (calc-enter-result 1 "pfac" res
))))
226 (defun calc-totient (arg)
229 (calc-unary-op "phi" 'calcFunc-totient arg
)))
231 (defun calc-moebius (arg)
234 (calc-unary-op "mu" 'calcFunc-moebius arg
)))
237 (defun calcFunc-gcd (a b
)
238 (if (Math-messy-integerp a
)
239 (setq a
(math-trunc a
)))
240 (if (Math-messy-integerp b
)
241 (setq b
(math-trunc b
)))
242 (cond ((and (Math-integerp a
) (Math-integerp b
))
245 (calcFunc-gcd (math-neg a
) b
))
247 (calcFunc-gcd a
(math-neg b
)))
252 (math-make-frac (math-gcd (if (eq (car-safe a
) 'frac
) (nth 1 a
) a
)
253 (if (eq (car-safe b
) 'frac
) (nth 1 b
) b
))
255 (if (eq (car-safe a
) 'frac
) (nth 2 a
) 1)
256 (if (eq (car-safe b
) 'frac
) (nth 2 b
) 1))))
257 ((not (Math-integerp a
))
258 (calc-record-why 'integerp a
)
259 (list 'calcFunc-gcd a b
))
261 (calc-record-why 'integerp b
)
262 (list 'calcFunc-gcd a b
))))
264 (defun calcFunc-lcm (a b
)
265 (let ((g (calcFunc-gcd a b
)))
267 (math-div (math-mul a b
) g
)
268 (list 'calcFunc-lcm a b
))))
270 (defun calcFunc-egcd (a b
) ; Knuth section 4.5.2
272 ((not (Math-integerp a
))
273 (if (Math-messy-integerp a
)
274 (calcFunc-egcd (math-trunc a
) b
)
275 (calc-record-why 'integerp a
)
276 (list 'calcFunc-egcd a b
)))
277 ((not (Math-integerp b
))
278 (if (Math-messy-integerp b
)
279 (calcFunc-egcd a
(math-trunc b
))
280 (calc-record-why 'integerp b
)
281 (list 'calcFunc-egcd a b
)))
283 (let ((u1 1) (u2 0) (u3 a
)
286 (while (not (eq v3
0))
287 (setq q
(math-idivmod u3 v3
)
288 t1
(math-sub u1
(math-mul v1
(car q
)))
289 t2
(math-sub u2
(math-mul v2
(car q
)))
291 v1 t1 v2 t2 v3
(cdr q
)))
292 (list 'vec u3 u1 u2
)))))
295 ;;; Factorial and related functions.
297 (defun calcFunc-fact (n) ; [I I] [F F] [Public]
299 (cond ((Math-integer-negp n
)
300 (if calc-infinite-mode
302 (math-reject-arg n
'range
)))
305 (aref '[1 1 2 6 24 120 720 5040 40320 362880
306 (bigpos 800 628 3) (bigpos 800 916 39)
307 (bigpos 600 1 479) (bigpos 800 20 227 6)
308 (bigpos 200 291 178 87) (bigpos 0 368 674 307 1)
309 (bigpos 0 888 789 922 20) (bigpos 0 96 428 687 355)
310 (bigpos 0 728 705 373 402 6)
311 (bigpos 0 832 408 100 645 121)
312 (bigpos 0 640 176 8 902 432 2)] n
)
313 (math-factorial-iter (1- n
) 2 1)))
314 ((and (math-messy-integerp n
)
316 (math-inexact-result)
317 (setq temp
(math-trunc n
))
320 (math-float (calcFunc-fact temp
))
321 (math-with-extra-prec 1
322 (math-factorial-iter (1- temp
) 2 '(float 1 0))))
323 (math-reject-arg n
'range
)))
325 (let* ((q (math-quarter-integer n
))
326 (tn (and q
(Math-lessp n
1000) (Math-lessp -
1000 n
)
327 (1+ (math-floor n
)))))
328 (cond ((and tn
(= q
2)
329 (or calc-symbolic-mode
(< (math-abs tn
) 20)))
330 (let ((q (if (< tn
0)
333 (math-double-factorial-iter (* -
2 tn
) 3 1 2))
335 (math-double-factorial-iter (* 2 tn
) 3 1 2)
337 (math-mul q
(if calc-symbolic-mode
338 (list 'calcFunc-sqrt
'(var pi var-pi
))
340 ((and tn
(>= tn
0) (< tn
20)
342 (math-inexact-result)
344 (math-mul (math-double-factorial-iter (* 4 tn
) q
1 4)
345 (if (= q
1) (math-gamma-1q) (math-gamma-3q)))
348 (math-inexact-result)
349 (math-with-extra-prec 3
350 (math-gammap1-raw (math-float n
)))))))
351 ((equal n
'(var inf var-inf
)) n
)
352 (t (calc-record-why 'numberp n
)
353 (list 'calcFunc-fact n
)))))
355 (math-defcache math-gamma-1q nil
356 (math-with-extra-prec 3
357 (math-gammap1-raw '(float -
75 -
2))))
359 (math-defcache math-gamma-3q nil
360 (math-with-extra-prec 3
361 (math-gammap1-raw '(float -
25 -
2))))
363 (defun math-factorial-iter (count n f
)
365 (math-working (format "factorial(%d)" (1- n
)) f
))
367 (math-factorial-iter (1- count
) (1+ n
) (math-mul n f
))
370 (defun calcFunc-dfact (n) ; [I I] [F F] [Public]
371 (cond ((Math-integer-negp n
)
375 (math-div (if (eq (math-mod n
4) 3) 1 -
1)
376 (calcFunc-dfact (math-sub -
2 n
))))
377 (list 'calcFunc-dfact n
)))
379 ((integerp n
) (math-double-factorial-iter n
(+ 2 (% n
2)) 1 2))
380 ((math-messy-integerp n
)
381 (let ((temp (math-trunc n
)))
382 (math-inexact-result)
384 (if (Math-lessp temp
200)
385 (math-with-extra-prec 1
386 (math-double-factorial-iter temp
(+ 2 (% temp
2))
388 (let* ((half (math-div2 temp
))
389 (even (math-mul (math-pow 2 half
)
390 (calcFunc-fact (math-float half
)))))
391 (if (math-evenp temp
)
393 (math-div (calcFunc-fact n
) even
))))
394 (list 'calcFunc-dfact n
))))
395 ((equal n
'(var inf var-inf
)) n
)
396 (t (calc-record-why 'natnump n
)
397 (list 'calcFunc-dfact n
))))
399 (defun math-double-factorial-iter (max n f step
)
400 (if (< (% n
12) step
)
401 (math-working (format "dfact(%d)" (- n step
)) f
))
403 (math-double-factorial-iter max
(+ n step
) (math-mul n f
) step
)
406 (defun calcFunc-perm (n m
) ; [I I I] [F F F] [Public]
407 (cond ((and (integerp n
) (integerp m
) (<= m n
) (>= m
0))
408 (math-factorial-iter m
(1+ (- n m
)) 1))
409 ((or (not (math-num-integerp n
))
410 (and (math-messy-integerp n
) (Math-lessp 100 n
))
411 (not (math-num-integerp m
))
412 (and (math-messy-integerp m
) (Math-lessp 100 m
)))
413 (or (math-realp n
) (equal n
'(var inf var-inf
))
414 (math-reject-arg n
'realp
))
415 (or (math-realp m
) (equal m
'(var inf var-inf
))
416 (math-reject-arg m
'realp
))
417 (and (math-num-integerp n
) (math-negp n
) (math-reject-arg n
'range
))
418 (and (math-num-integerp m
) (math-negp m
) (math-reject-arg m
'range
))
419 (math-div (calcFunc-fact n
) (calcFunc-fact (math-sub n m
))))
421 (let ((tn (math-trunc n
))
423 (math-inexact-result)
424 (or (integerp tn
) (math-reject-arg tn
'fixnump
))
425 (or (integerp tm
) (math-reject-arg tm
'fixnump
))
426 (or (and (<= tm tn
) (>= tm
0)) (math-reject-arg tm
'range
))
427 (math-with-extra-prec 1
428 (math-factorial-iter tm
(1+ (- tn tm
)) '(float 1 0)))))))
430 (defun calcFunc-choose (n m
) ; [I I I] [F F F] [Public]
431 (cond ((and (integerp n
) (integerp m
) (<= m n
) (>= m
0))
433 (math-choose-iter (- n m
) n
1 1)
434 (math-choose-iter m n
1 1)))
435 ((not (math-realp n
))
436 (math-reject-arg n
'realp
))
437 ((not (math-realp m
))
438 (math-reject-arg m
'realp
))
439 ((not (math-num-integerp m
))
440 (if (and (math-num-integerp n
) (math-negp n
))
441 (list 'calcFunc-choose n m
)
442 (math-div (calcFunc-fact (math-float n
))
443 (math-mul (calcFunc-fact m
)
444 (calcFunc-fact (math-sub n m
))))))
447 (let ((val (calcFunc-choose (math-add (math-add n m
) -
1) m
)))
448 (if (math-evenp (math-trunc m
))
451 ((and (math-num-integerp n
)
455 (math-inexact-result)
456 (let ((tm (math-trunc m
)))
457 (or (integerp tm
) (math-reject-arg tm
'fixnump
))
459 (math-div (calcFunc-fact (math-float n
))
460 (math-mul (calcFunc-fact (math-float m
))
461 (calcFunc-fact (math-float
463 (math-with-extra-prec 1
464 (math-choose-float-iter tm n
1 1)))))))
466 (defun math-choose-iter (m n i c
)
467 (if (and (= (% i
5) 1) (> i
5))
468 (math-working (format "choose(%d)" (1- i
)) c
))
470 (math-choose-iter m
(1- n
) (1+ i
)
471 (math-quotient (math-mul c n
) i
))
474 (defun math-choose-float-iter (count n i c
)
476 (math-working (format "choose(%d)" (1- i
)) c
))
478 (math-choose-float-iter (1- count
) (math-sub n
1) (1+ i
)
479 (math-div (math-mul c n
) i
))
483 ;;; Stirling numbers.
485 (defun calcFunc-stir1 (n m
)
486 (math-stirling-number n m
1))
488 (defun calcFunc-stir2 (n m
)
489 (math-stirling-number n m
0))
491 (defvar math-stirling-cache
(vector [[1]] [[1]]))
493 ;; The variable math-stirling-local-cache is local to
494 ;; math-stirling-number, but is used by math-stirling-1
495 ;; and math-stirling-2, which are called by math-stirling-number.
496 (defvar math-stirling-local-cache
)
498 (defun math-stirling-number (n m k
)
499 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
500 (or (math-num-natnump m
) (math-reject-arg m
'natnump
))
501 (if (consp n
) (setq n
(math-trunc n
)))
502 (or (integerp n
) (math-reject-arg n
'fixnump
))
503 (if (consp m
) (setq m
(math-trunc m
)))
504 (or (integerp m
) (math-reject-arg m
'fixnump
))
507 (let ((math-stirling-local-cache (aref math-stirling-cache k
)))
508 (while (<= (length math-stirling-local-cache
) n
)
509 (let ((i (1- (length math-stirling-local-cache
)))
511 (setq math-stirling-local-cache
512 (vconcat math-stirling-local-cache
513 (make-vector (length math-stirling-local-cache
) nil
)))
514 (aset math-stirling-cache k math-stirling-local-cache
)
515 (while (< (setq i
(1+ i
)) (length math-stirling-local-cache
))
516 (aset math-stirling-local-cache i
(setq row
(make-vector (1+ i
) nil
)))
520 (math-stirling-1 n m
)
521 (math-stirling-2 n m
)))))
523 (defun math-stirling-1 (n m
)
524 (or (aref (aref math-stirling-local-cache n
) m
)
525 (aset (aref math-stirling-local-cache n
) m
526 (math-add (math-stirling-1 (1- n
) (1- m
))
527 (math-mul (- 1 n
) (math-stirling-1 (1- n
) m
))))))
529 (defun math-stirling-2 (n m
)
530 (or (aref (aref math-stirling-local-cache n
) m
)
531 (aset (aref math-stirling-local-cache n
) m
532 (math-add (math-stirling-2 (1- n
) (1- m
))
533 (math-mul m
(math-stirling-2 (1- n
) m
))))))
535 (defvar math-random-table nil
)
536 (defvar math-last-RandSeed nil
)
537 (defvar math-random-ptr1 nil
)
538 (defvar math-random-ptr2 nil
)
539 (defvar math-random-shift nil
)
541 ;;; Produce a random 10-bit integer, with (random) if no seed provided,
542 ;;; or else with Numerical Recipes algorithm ran3 / Knuth 3.2.2-A.
544 (defvar var-RandSeed
)
545 (defvar math-random-cache nil
)
546 (defvar math-gaussian-cache nil
)
548 (defun math-init-random-base ()
549 (if (and (boundp 'var-RandSeed
) var-RandSeed
)
550 (if (eq (car-safe var-RandSeed
) 'vec
)
552 (if (Math-integerp var-RandSeed
)
553 (let* ((seed (math-sub 161803 var-RandSeed
))
554 (mj (1+ (math-mod seed
'(bigpos 0 0 1))))
555 (mk (1+ (math-mod (math-quotient seed
'(bigpos 0 0 1))
558 (setq math-random-table
(cons 'vec
(make-list 55 mj
)))
559 (while (<= (setq i
(1+ i
)) 54)
560 (let* ((ii (%
(* i
21) 55))
561 (p (nthcdr ii math-random-table
)))
565 (math-reject-arg var-RandSeed
"*RandSeed must be an integer"))
566 (setq var-RandSeed
(list 'vec var-RandSeed
)
567 math-random-ptr1 math-random-table
568 math-random-cache nil
569 math-random-ptr2
(nthcdr 31 math-random-table
))
571 (while (> (setq i
(1- i
)) 0)
572 (math-random-base))))
574 (setq var-RandSeed nil
575 math-random-cache nil
576 math-random-shift -
4) ; assume RAND_MAX >= 16383
577 ;; This exercises the random number generator and also helps
578 ;; deduce a better value for RAND_MAX.
580 (while (< (setq i
(1+ i
)) 30)
581 (if (> (lsh (math-abs (random)) math-random-shift
) 4095)
582 (setq math-random-shift
(1- math-random-shift
))))))
583 (setq math-last-RandSeed var-RandSeed
584 math-gaussian-cache nil
))
586 (defun math-random-base ()
589 (setq math-random-ptr1
(or (cdr math-random-ptr1
)
590 (cdr math-random-table
))
591 math-random-ptr2
(or (cdr math-random-ptr2
)
592 (cdr math-random-table
)))
593 (logand (lsh (setcar math-random-ptr1
594 (logand (- (car math-random-ptr1
)
595 (car math-random-ptr2
)) 524287))
597 (logand (lsh (random) math-random-shift
) 1023)))
600 ;;; Produce a random digit in the range 0..999.
601 ;;; Avoid various pitfalls that may lurk in the built-in (random) function!
602 ;;; Shuffling algorithm from Numerical Recipes, section 7.1.
603 (defvar math-random-last
)
604 (defun math-random-digit ()
606 (or (and (boundp 'var-RandSeed
) (eq var-RandSeed math-last-RandSeed
))
607 (math-init-random-base))
608 (or math-random-cache
610 (setq math-random-last
(math-random-base)
611 math-random-cache
(make-vector 13 nil
)
613 (while (< (setq i
(1+ i
)) 13)
614 (aset math-random-cache i
(math-random-base)))))
616 (setq i
(/ math-random-last
79) ; 0 <= i < 13
617 math-random-last
(aref math-random-cache i
))
618 (aset math-random-cache i
(math-random-base))
619 (>= math-random-last
1000)))
622 ;;; Produce an N-digit random integer.
623 (defun math-random-digits (n)
625 (math-scale-right (+ (* (math-random-digit) 1000) (math-random-digit))
627 (t (let* ((slop (%
(- 900003 n
) 3))
631 (setq digs
(cons (math-random-digit) digs
)
633 (math-normalize (math-scale-right (cons 'bigpos digs
)
636 ;;; Produce a uniformly-distributed random float 0 <= N < 1.
637 (defun math-random-float ()
638 (math-make-float (math-random-digits calc-internal-prec
)
639 (- calc-internal-prec
)))
641 ;;; Produce a Gaussian-distributed random float with mean=0, sigma=1.
642 (defun math-gaussian-float ()
643 (math-with-extra-prec 2
644 (if (and math-gaussian-cache
645 (= (car math-gaussian-cache
) calc-internal-prec
))
647 (cdr math-gaussian-cache
)
648 (setq math-gaussian-cache nil
))
649 (let* ((v1 (math-add (math-mul (math-random-float) 2) -
1))
650 (v2 (math-add (math-mul (math-random-float) 2) -
1))
651 (r (math-add (math-sqr v1
) (math-sqr v2
))))
652 (while (or (not (Math-lessp r
1)) (math-zerop r
))
653 (setq v1
(math-add (math-mul (math-random-float) 2) -
1)
654 v2
(math-add (math-mul (math-random-float) 2) -
1)
655 r
(math-add (math-sqr v1
) (math-sqr v2
))))
656 (let ((fac (math-sqrt (math-mul (math-div (calcFunc-ln r
) r
) -
2))))
657 (setq math-gaussian-cache
(cons calc-internal-prec
659 (math-mul v2 fac
))))))
661 ;;; Produce a random integer or real 0 <= N < MAX.
662 (defun calcFunc-random (max)
663 (cond ((Math-zerop max
)
664 (math-gaussian-float))
666 (let* ((digs (math-numdigs max
))
667 (r (math-random-digits (+ digs
3))))
670 (math-mul (math-random-float) max
))
671 ((and (eq (car max
) 'intv
) (math-constp max
)
672 (Math-lessp (nth 2 max
) (nth 3 max
)))
673 (if (math-floatp max
)
674 (let ((val (math-add (math-mul (math-random-float)
675 (math-sub (nth 3 max
) (nth 2 max
)))
677 (if (or (and (memq (nth 1 max
) '(0 1)) ; almost not worth
678 (Math-equal val
(nth 2 max
))) ; checking!
679 (and (memq (nth 1 max
) '(0 2))
680 (Math-equal val
(nth 3 max
))))
681 (calcFunc-random max
)
683 (let ((lo (if (memq (nth 1 max
) '(0 1))
684 (math-add (nth 2 max
) 1) (nth 2 max
)))
685 (hi (if (memq (nth 1 max
) '(1 3))
686 (math-add (nth 3 max
) 1) (nth 3 max
))))
687 (if (Math-lessp lo hi
)
688 (math-add (calcFunc-random (math-sub hi lo
)) lo
)
689 (math-reject-arg max
"*Empty interval")))))
692 (nth (1+ (calcFunc-random (1- (length max
)))) max
)
693 (math-reject-arg max
"*Empty list")))
694 ((and (eq (car max
) 'sdev
) (math-constp max
) (Math-realp (nth 1 max
)))
695 (math-add (math-mul (math-gaussian-float) (nth 2 max
)) (nth 1 max
)))
696 (t (math-reject-arg max
'realp
))))
698 ;;; Choose N objects at random from the set MAX without duplicates.
699 (defun calcFunc-shuffle (n &optional max
)
700 (or max
(setq max n n -
1))
701 (or (and (Math-num-integerp n
)
702 (or (natnump (setq n
(math-trunc n
))) (eq n -
1)))
703 (math-reject-arg n
'integerp
))
704 (cond ((or (math-zerop max
)
706 (eq (car-safe max
) 'sdev
))
708 (math-reject-arg n
'natnump
)
709 (math-simple-shuffle n max
)))
710 ((and (<= n
1) (>= n
0))
711 (math-simple-shuffle n max
))
712 ((and (eq (car-safe max
) 'intv
) (math-constp max
))
713 (let ((num (math-add (math-sub (nth 3 max
) (nth 2 max
))
714 (cdr (assq (nth 1 max
)
717 (min (math-add (nth 2 max
) (if (memq (nth 1 max
) '(0 1))
719 (if (< n
0) (setq n num
))
720 (or (math-posp num
) (math-reject-arg max
'range
))
721 (and (Math-lessp num n
) (math-reject-arg n
'range
))
722 (if (Math-lessp n
(math-quotient num
3))
723 (math-simple-shuffle n max
)
724 (if (> (* n
4) (* num
3))
725 (math-add (math-sub min
1)
726 (math-shuffle-list n num
(calcFunc-index num
)))
731 (if (< (calcFunc-random (- num tot
)) (- n m
))
732 (setq vec
(cons (math-add min tot
) vec
)
735 (math-shuffle-list n n
(cons 'vec vec
)))))))
736 ((eq (car-safe max
) 'vec
)
737 (let ((size (1- (length max
))))
738 (if (< n
0) (setq n size
))
739 (if (and (> n
(/ size
2)) (<= n size
))
740 (math-shuffle-list n size
(copy-sequence max
))
741 (let* ((vals (calcFunc-shuffle
742 n
(list 'intv
3 1 (1- (length max
)))))
744 (while (setq p
(cdr p
))
745 (setcar p
(nth (car p
) max
)))
749 (calcFunc-shuffle n
(list 'intv
2 0 max
))
750 (calcFunc-shuffle n
(list 'intv
1 max
0))))
751 (t (math-reject-arg max
'realp
))))
753 (defun math-simple-shuffle (n max
)
756 (while (>= (setq n
(1- n
)) 0)
757 (while (math-member (setq val
(calcFunc-random max
)) vec
))
758 (setq vec
(cons val vec
)))
761 (defun math-shuffle-list (n size vec
)
765 (while (cdr (setq p
(cdr p
)))
766 (setq k
(calcFunc-random j
)
769 (setcar (nthcdr k p
) (car p
))
771 (cons 'vec
(nthcdr (- size n -
1) vec
))))
773 (defun math-member (x list
)
774 (while (and list
(not (equal x
(car list
))))
775 (setq list
(cdr list
)))
779 ;;; Check if the integer N is prime. [X I]
780 ;;; Return (nil) if non-prime,
781 ;;; (nil N) if non-prime with known factor N,
782 ;;; (nil unknown) if non-prime with no known factors,
784 ;;; (maybe N P) if probably prime (after N iters with probability P%)
785 (defvar math-prime-test-cache
'(-1))
787 (defvar math-prime-test-cache-k
)
788 (defvar math-prime-test-cache-q
)
789 (defvar math-prime-test-cache-nm1
)
791 (defun math-prime-test (n iters
)
792 (if (and (Math-vectorp n
) (cdr n
))
793 (setq n
(nth (1- (length n
)) n
)))
794 (if (Math-messy-integerp n
)
795 (setq n
(math-trunc n
)))
799 (cond ((and (integerp n
) (<= n
5003))
800 (list (= (math-next-small-prime n
) n
)))
801 ((not (Math-integerp n
))
802 (error "Argument must be an integer"))
803 ((Math-integer-negp n
)
805 ((Math-natnum-lessp n
'(bigpos 0 0 8))
806 (setq n
(math-fixnum n
))
808 (while (and (> (% n
(setq v
(aref math-primes-table
815 ((not (equal n
(car math-prime-test-cache
)))
816 (cond ((= (%
(nth 1 n
) 2) 0) '(nil 2))
817 ((= (%
(nth 1 n
) 5) 0) '(nil 5))
818 (t (let ((dig (cdr n
)) (sum 0))
821 (setq sum
(%
(+ (+ sum
(car dig
))
822 (* (nth 1 dig
) 1000))
825 (setq sum
(%
(+ sum
(car dig
)) 111111)
827 (cond ((= (% sum
3) 0) '(nil 3))
828 ((= (% sum
7) 0) '(nil 7))
829 ((= (% sum
11) 0) '(nil 11))
830 ((= (% sum
13) 0) '(nil 13))
831 ((= (% sum
37) 0) '(nil 37))
833 (setq math-prime-test-cache-k
1
834 math-prime-test-cache-q
836 math-prime-test-cache-nm1
839 math-prime-test-cache-q
)
840 (setq math-prime-test-cache-k
841 (1+ math-prime-test-cache-k
)
842 math-prime-test-cache-q
844 math-prime-test-cache-q
)))
845 (setq iters
(1+ iters
))
852 (math-numdigs n
))))))))))
853 ((not (eq (car (nth 1 math-prime-test-cache
)) 'maybe
))
854 (nth 1 math-prime-test-cache
))
856 (let* ((x (math-add (calcFunc-random (math-add n -
2)) 2))
857 (y (math-pow-mod x math-prime-test-cache-q n
))
859 (while (and (not (eq y
1))
860 (not (equal y math-prime-test-cache-nm1
))
861 (< (setq j
(1+ j
)) math-prime-test-cache-k
))
862 (setq y
(math-mod (math-mul y y
) n
)))
863 (if (or (equal y math-prime-test-cache-nm1
)
864 (and (eq y
1) (eq j
0)))
866 (1+ (nth 1 (nth 1 math-prime-test-cache
)))
867 (math-mul (nth 2 (nth 1 math-prime-test-cache
))
870 (setq math-prime-test-cache
(list n res
)
871 iters
(if (eq (car res
) 'maybe
)
876 (defun calcFunc-prime (n &optional iters
)
877 (or (math-num-integerp n
) (math-reject-arg n
'integerp
))
878 (or (not iters
) (math-num-integerp iters
) (math-reject-arg iters
'integerp
))
879 (if (car (math-prime-test (math-trunc n
) (math-trunc (or iters
1))))
883 ;;; Theory: summing base-10^6 digits modulo 111111 is "casting out 999999s".
884 ;;; Initial probability that N is prime is 1/ln(N) = log10(e)/log10(N).
885 ;;; After culling [2,3,5,7,11,13,37], probability of primality is 5.36 x more.
886 ;;; Initial reported probability of non-primality is thus 100% - this.
887 ;;; Each Fermat step multiplies this probability by 25%.
888 ;;; The Fermat step is algorithm P from Knuth section 4.5.4.
891 (defun calcFunc-prfac (n)
892 (setq math-prime-factors-finished t
)
893 (if (Math-messy-integerp n
)
894 (setq n
(math-trunc n
)))
896 (if (Math-natnum-lessp 2 n
)
897 (let (factors res p
(i 0))
898 (while (and (not (eq n
1))
899 (< i
(length math-primes-table
)))
900 (setq p
(aref math-primes-table i
))
901 (while (eq (cdr (setq res
(cond ((eq n p
) (cons 1 0))
902 ((eq n
1) (cons 0 1))
903 ((consp n
) (math-idivmod n p
))
904 (t (cons (/ n p
) (% n p
))))))
906 (math-working "factor" p
)
907 (setq factors
(nconc factors
(list p
))
910 (Math-natnum-lessp p
(car res
))
911 (setq factors
(nconc factors
(list n
))
914 (or (setq math-prime-factors-finished
(eq n
1))
915 (setq factors
(nconc factors
(list n
))))
918 (if (Math-integerp n
)
921 (cons 'vec
(cons -
1 (cdr (calcFunc-prfac (math-neg n
))))))
922 (calc-record-why 'integerp n
)
923 (list 'calcFunc-prfac n
))))
925 (defun calcFunc-totient (n)
926 (if (Math-messy-integerp n
)
927 (setq n
(math-trunc n
)))
929 (if (Math-natnum-lessp n
2)
931 (calcFunc-totient (math-abs n
))
933 (let ((factors (cdr (calcFunc-prfac n
)))
935 (if math-prime-factors-finished
938 (setq p
(car factors
)
939 n
(math-mul (math-div n p
) (math-add p -
1)))
940 (while (equal p
(car factors
))
941 (setq factors
(cdr factors
))))
943 (calc-record-why "*Number too big to factor" n
)
944 (list 'calcFunc-totient n
))))
945 (calc-record-why 'natnump n
)
946 (list 'calcFunc-totient n
)))
948 (defun calcFunc-moebius (n)
949 (if (Math-messy-integerp n
)
950 (setq n
(math-trunc n
)))
951 (if (and (Math-natnump n
) (not (eq n
0)))
952 (if (Math-natnum-lessp n
2)
954 (calcFunc-moebius (math-abs n
))
956 (let ((factors (cdr (calcFunc-prfac n
)))
958 (if math-prime-factors-finished
961 (setq mu
(if (equal (car factors
) (nth 1 factors
))
963 factors
(cdr factors
)))
965 (calc-record-why "Number too big to factor" n
)
966 (list 'calcFunc-moebius n
))))
967 (calc-record-why 'posintp n
)
968 (list 'calcFunc-moebius n
)))
971 (defun calcFunc-nextprime (n &optional iters
)
972 (if (Math-integerp n
)
973 (if (Math-integer-negp n
)
975 (if (and (integerp n
) (< n
5003))
976 (math-next-small-prime (1+ n
))
978 (setq n
(math-add n -
1)))
980 (while (not (car (setq res
(math-prime-test
981 (setq n
(math-add n
2))
983 (if (and calc-verbose-nextprime
984 (eq (car res
) 'maybe
))
985 (calc-report-prime-test res
)))
988 (calcFunc-nextprime (math-trunc n
) iters
)
989 (math-reject-arg n
'integerp
))))
991 (defun calcFunc-prevprime (n &optional iters
)
992 (if (Math-integerp n
)
996 (setq n
(math-add n
1)))
998 (while (not (car (setq res
(math-prime-test
999 (setq n
(math-add n -
2))
1001 (if (and calc-verbose-nextprime
1002 (eq (car res
) 'maybe
))
1003 (calc-report-prime-test res
)))
1006 (calcFunc-prevprime (math-ceiling n
) iters
)
1007 (math-reject-arg n
'integerp
))))
1009 (defun math-next-small-prime (n)
1010 (if (and (integerp n
) (> n
2))
1012 (hi (length math-primes-table
))
1014 (while (> (- hi lo
) 1)
1015 (if (> n
(aref math-primes-table
1016 (setq mid
(ash (+ lo hi
) -
1))))
1019 (aref math-primes-table hi
))
1022 (provide 'calc-comb
)
1024 ;;; arch-tag: 1d75ee9b-0815-42bd-a321-bb3dc001cc02
1025 ;;; calc-comb.el ends here