I've started reading SICP and I came across the Fermat primality test implemented an Scheme. I reimplemented it in Clojure and was able to switch the recursive call in fast-prime to TCO/recur, but I was unable to do the same for the exp-mod function.
(defn exp-mod [base exp m] (cond (zero? exp) 1 (even? exp) (mod (Math/sqrt (exp-mod base (/ exp 2) m)) m) :else (mod (* base (exp-mod base (inc exp) m)) m))) (defn fermat-test [n] (defn try-it [a] (= (exp-mod a n n) a)) (try-it (inc (rand-int (dec n))))) (defn fast-prime? [n times] (cond (zero? times) true (fermat-test n) (recur n (dec times)) :else false)) Calling (fast-prime? 5 3) blows the stack. How can I change exp-mod to use TCO? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---