How to do exponentiation in clojure?

How can I do exponentiation in clojure? For now I'm only needing integer exponentiation, but the question goes for fractions too.


Solution 1:

classic recursion (watch this, it blows stack)

(defn exp [x n]
     (if (zero? n) 1
         (* x (exp x (dec n)))))

tail recursion

(defn exp [x n]
  (loop [acc 1 n n]
    (if (zero? n) acc
        (recur (* x acc) (dec n)))))

functional

(defn exp [x n]
  (reduce * (repeat n x)))

sneaky (also blows stack, but not so easily)

(defn exp-s [x n]
  (let [square (fn[x] (* x x))]
    (cond (zero? n) 1
          (even? n) (square (exp-s x (/ n 2)))
          :else (* x (exp-s x (dec n))))))

library

(require 'clojure.contrib.math)

Solution 2:

Clojure has a power function that works well: I'd recommend using this rather than going via Java interop since it handles all the Clojure arbitrary-precision number types correctly. It is in namespace clojure.math.numeric-tower.

It's called expt for exponentiation rather than power or pow which maybe explains why it's a bit hard to find ... anyway here's a small example (note that use works but better use require):

(require '[clojure.math.numeric-tower :as math :refer [expt]])  ; as of Clojure 1.3
;; (use 'clojure.contrib.math)     ; before Clojure 1.3
(expt 2 200)
=> 1606938044258990275541962092341162602522202993782792835301376

Reminder about package installation

You must first install the Java package org.clojure.math.numeric-tower to make the Clojure namespace clojure.math.numeric-tower accessible!

On the command line:

$ lein new my-example-project
$ cd lein new my-example-project

Then edit project.clj and add [org.clojure/math.numeric-tower "0.0.4"] to the dependencies vector.

Start a lein REPL (not a clojure REPL)

$ lein repl

Now:

(require '[clojure.math.numeric-tower :as math])
(math/expt 4 2)
;=> 16

or

(require '[clojure.math.numeric-tower :as math :refer [expt]])
(expt 4 2)
;=> 16

Solution 3:

You can use java's Math.pow or BigInteger.pow methods:

(Math/pow base exponent)

(.pow (bigdec base) exponent)

Solution 4:

When this question was originally asked, clojure.contrib.math/expt was the official library function to do this. Since then, it has moved to clojure.math.numeric-tower