Here I use tail recursion, which does not give you a stack overflow.
(ns factorial) (defn get-factorial ([n] (get-factorial n 1)) ([n part] (if (> n 1) (recur (dec n) (* part n)) part)))
- (ns factorial)
(defn get-factorial [n]"takes n as a paramenter and returns n!"(if (<= n 0) 0)(if (<= n 1) 1(* n (get-factorial (dec n)))))- (defn get-factorial
- ([n]
- (get-factorial n 1))
- ([n part]
- (if (> n 1)
- (recur (dec n) (* part n))
- part)))
You do not have to multiply by 1.