emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master d831312: Use iteration in math-factorial-iter


From: Paul Eggert
Subject: [Emacs-diffs] master d831312: Use iteration in math-factorial-iter
Date: Tue, 18 Apr 2017 14:25:18 -0400 (EDT)

branch: master
commit d831312d668fbedeffe3711b13cd455309da84a1
Author: michael schuldt <address@hidden>
Commit: Paul Eggert <address@hidden>

    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
---
 lisp/calc/calc-comb.el | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lisp/calc/calc-comb.el b/lisp/calc/calc-comb.el
index c84ff23..91fbb7b 100644
--- a/lisp/calc/calc-comb.el
+++ b/lisp/calc/calc-comb.el
@@ -362,11 +362,13 @@
     (math-gammap1-raw '(float -25 -2))))
 
 (defun math-factorial-iter (count n f)
-  (if (= (% n 5) 1)
-      (math-working (format "factorial(%d)" (1- n)) f))
-  (if (> count 0)
-      (math-factorial-iter (1- count) (1+ n) (math-mul n f))
-    f))
+  (while (> count 0)
+    (if (= (% n 5) 1)
+       (math-working (format "factorial(%d)" (1- n)) f))
+    (setq count (1- count)
+         f (math-mul n f)
+         n (1+ n)))
+  f)
 
 (defun calcFunc-dfact (n)   ; [I I] [F F] [Public]
   (cond ((Math-integer-negp n)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]