diff options
| author | michael schuldt | 2017-04-18 11:24:37 -0700 |
|---|---|---|
| committer | Paul Eggert | 2017-04-18 11:25:13 -0700 |
| commit | d831312d668fbedeffe3711b13cd455309da84a1 (patch) | |
| tree | 9b5a0e0fb0e27784ca8b3af2e9db93a4a1169a99 | |
| parent | 0c98dec5c9d0e0146a1519b9a7e099aae668c059 (diff) | |
| download | emacs-d831312d668fbedeffe3711b13cd455309da84a1.tar.gz emacs-d831312d668fbedeffe3711b13cd455309da84a1.zip | |
Use iteration in math-factorial-iter
* lisp/calc/calc-comb.el (math-factorial-iter):
Use iteration instead of recursion to avoid max-specpdl-size problem.
Copyright-paperwork-exempt: yes
| -rw-r--r-- | lisp/calc/calc-comb.el | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/lisp/calc/calc-comb.el b/lisp/calc/calc-comb.el index c84ff236851..91fbb7b2b8a 100644 --- a/lisp/calc/calc-comb.el +++ b/lisp/calc/calc-comb.el | |||
| @@ -362,11 +362,13 @@ | |||
| 362 | (math-gammap1-raw '(float -25 -2)))) | 362 | (math-gammap1-raw '(float -25 -2)))) |
| 363 | 363 | ||
| 364 | (defun math-factorial-iter (count n f) | 364 | (defun math-factorial-iter (count n f) |
| 365 | (if (= (% n 5) 1) | 365 | (while (> count 0) |
| 366 | (math-working (format "factorial(%d)" (1- n)) f)) | 366 | (if (= (% n 5) 1) |
| 367 | (if (> count 0) | 367 | (math-working (format "factorial(%d)" (1- n)) f)) |
| 368 | (math-factorial-iter (1- count) (1+ n) (math-mul n f)) | 368 | (setq count (1- count) |
| 369 | f)) | 369 | f (math-mul n f) |
| 370 | n (1+ n))) | ||
| 371 | f) | ||
| 370 | 372 | ||
| 371 | (defun calcFunc-dfact (n) ; [I I] [F F] [Public] | 373 | (defun calcFunc-dfact (n) ; [I I] [F F] [Public] |
| 372 | (cond ((Math-integer-negp n) | 374 | (cond ((Math-integer-negp n) |