对吗?实践出真知,运行看看。代码如下。
(defn square [x] (* x x))
(defn fast-expt[b n]
(println "call iter" n)
(cond (= 1 n) b
(= 2 n) (square b)
(even? n) (square (fast-expt b (/ n 2)))
:else (* b (fast-expt b (- n 1)))
)
)
(defn expmod [base exp m]
(mod (fast-expt base exp) m)
)
再看看运行结果
这执行结果有点奇怪,需要再次检查代码逻辑及输出.
(defn divisor? [ a b](= (mod b a) 0 ))
(defn square [x](* x x ))
(defn find-divisor [n test-divisor]
(cond (> (square test-divisor) n) n
(divisor? test-divisor n) test-divisor
:else (find-divisor n (+ test-divisor 1))
)