How to do exponentiation in clojure?

How can I make an exposition in clojure? At the moment, I only need integer exponentiation, but the question is about fractions.

+92
clojure exponentiation
Feb 20 '11 at 12:32
source share
13 answers

classic recursion (look, this hits the stack)

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

tail recursion

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

functionally

 (defn exp [xn] (reduce * (repeat nx))) 

sneaky (also hits the stack, but not so easy)

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

Library

 (require 'clojure.contrib.math) 
+127
Feb 20 '11 at 17:07
source share

Clojure has a powerful feature that works well: I would recommend using it instead of interacting with Java, since it correctly processes all Clojure numeric types with arbitrary precision.

He called expt to exponentiation rather than power or pow which perhaps explains why it is a little hard to find ... anyway, here is a small example:

 (use 'clojure.math.numeric-tower) ; as of Clojure 1.3 ;; (use 'clojure.contrib.math) ; before Clojure 1.3 (expt 2 200) => 1606938044258990275541962092341162602522202993782792835301376 
+74
Feb 22 '11 at 13:13
source share

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

 (Math/pow base exponent) (.pow (bigint base) exponent) 
+52
Feb 20 '11 at 12:38
source share

When this question was originally asked, clojure.contrib.math / expt was the official library function for this. He has since moved to clojure.math.numeric-tower

+11
Feb 20 '11 at 17:00
source share
 user=> (.pow (BigInteger. "2") 10) 1024 user=> (.pow (BigInteger. "2") 100) 1267650600228229401496703205376 
+7
Oct 27 '13 at 13:54 on
source share

If you really need a function, not a method, you can simply wrap it:

  (defn pow [be] (Math/pow be)) 

And in this function you can apply it to int or the like. Functions are often more useful in that methods, because you can pass them as parameters to other functions - in this case map comes to my mind.

If you really need to avoid interacting with Java, you can write your own nutrition function. For example, this is a simple function:

  (defn pow [np] (let [result (apply * (take (abs p) (cycle [n])))] (if (neg? p) (/ 1 result) result))) 

This calculates the power for an integer exponent (i.e. no roots).

Also, if you are dealing with large numbers, you can use BigInteger instead of int .

And if you are dealing with very large numbers, you can express them as lists of numbers and write your own arithmetic functions for streaming through them, since they calculate the result and output the result to some other stream.

+5
Feb 20 '11 at 2:00
source share

I think this will work too:

 (defn expt [x pow] (apply * (repeat pow x))) 
+4
Oct 12 2018-11-11T00:
source share

SICP inspired the full iterative quick version of the “hidden” implementation above.

 (defn fast-expt-iter [bn] (let [inner (fn [abn] (cond (= n 0) a (even? n) (recur a (* bb) (/ n 2)) :else (recur (* ab) b (- n 1)))) ] (inner 1 bn))) 
+2
Apr 10 '14 at 2:36
source share

Use clojure.math.numeric-tower , formerly clojure.contrib.math .




API Documentation




 (ns user (:require [clojure.math.numeric-tower :as m])) (defn- sqr "Uses the numeric tower expt to square a number" [x] (m/expt x 2)) 
+2
Jul 30 '15 at 18:03
source share

Implementing a "hidden" method with tail recursion and supporting a negative metric:

 (defn exp "exponent of x^n (int n only), with tail recursion and O(logn)" [xn] (if (< n 0) (/ 1 (exp x (- n))) (loop [acc 1 base x pow n] (if (= pow 0) acc (if (even? pow) (recur acc (* base base) (/ pow 2)) (recur (* acc base) base (dec pow))))))) 
+2
Feb 06 '16 at 13:53 on
source share

Simple single-line using reduction:

 (defn pow [ab] (reduce * 1 (repeat ba))) 
+2
Apr 11 '16 at 11:41
source share

Try

 (defn pow [xn] (loop [xxnnr 1] (cond (= n 0) r (even? n) (recur (* xx) (/ n 2) r) :else (recur x (dec n) (* rx))))) 

for a tail recursive solution O (log n) if you want to implement it yourself (supports only positive integers). Obviously, the best solution is to use the library functions that others have indicated.

+1
Feb 29 2018-12-12T00:
source share

How about clojure.contrib.genric.math-functions

There is a pow function in the clojure.contrib.generic.math-functions library. This is just a macro for Math.pow, and is a more “clairistic” way to call a Java mathematical function.

http://clojure.github.com/clojure-contrib/generic.math-functions-api.html#clojure.contrib.generic.math-functions/pow

0
Feb 20 2018-11-21T00:
source share



All Articles