# [Lisp] Compute factorial in Clojure

Page 1 of 1

## 0 Replies - 1711 Views - Last Post: 13 December 2009 - 09:17 AM

### #1 YamNad Reputation: 9
• Posts: 120
• Joined: 11-July 09

# [Lisp] Compute factorial in Clojure

Posted 13 December 2009 - 09:17 AM

Description: example usage: user=> (factorial 10) 3628800This function computes the factorial of n in Clojure.
```(defn factorial
"Computes the factorial of n."
[n]
(if (< n 0) (throw (Exception. "Cannot find the factorial of a negative number.")))
(if (zero? 0) 1
(* n (factorial (dec n)))))
```

Is This A Good Question/Topic? 0

## Replies To: [Lisp] Compute factorial in Clojure

### #2 YamNad Reputation: 9
• Posts: 120
• Joined: 11-July 09

## Re: [Lisp] Compute factorial in Clojure

Posted 13 December 2009 - 09:17 AM

Description: example usage: user=> (factorial 10) 3628800This function computes the factorial of n in Clojure.
```(defn factorial
"Computes the factorial of n."
[n]
(if (< n 0) (throw (Exception. "Cannot find the factorial of a negative number.")))
(if (zero? n) 1
(* n (factorial (dec n)))))

(def factorial (memoize factorial))
```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }