Ad

Here I use tail recursion, which does not give you a stack overflow.

Code
Diff
  • (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.

Code
Diff
  • (ns factorial.core)
    
    (defn factorial
      [n]
      (->> (inc n)
           (range 2)
           (reduce *)))
    
    • (ns factorial.core)
    • (defn factorial
    • [n]
    • (->> (inc n)
    • (range 1)
    • (range 2)
    • (reduce *)))