On Mon, Oct 1, 2012 at 6:29 PM, Mark Engelberg <mark.engelb...@gmail.com>wrote:

> (defn efficient-pow [base pow]
>   (loop [n pow, y 1, z base]
>     (let [t (even? n), n (quot n 2)]
>       (cond
>        t (recur n y (*' z z))
>        (zero? n) (*' z y)
>        :else (recur n (*' z y) (*' z z))))))
>
> This is essentially how I coded it in clojure.math.numeric-tower.
>

I should clarify that in clojure.math.numeric-tower, this is a helper
function called when the power is known to be positive, so this doesn't
work for a power of 0, for example.

It's written a little oddly because the variable names and branching
structure are meant to reflect the algorithm as presented by Knuth.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to