DEV Community

Discussion on: Daily Challenge #231 - Perfect Powers

Collapse
 
quoll profile image
Paula Gearon • Edited

Not necessarily the best approach, but this works in Clojure

(defn pp? [x]
  (let [logx (Math/log x)
        range-to #(range 2 (inc %))
        power? (fn [m]
                 (when-let [k (some
                                #(if (= x (long (Math/pow m %))) %)
                                (range-to (/ logx (Math/log m))))]
                   [m k]))]
    (some power? (range-to (Math/sqrt x)))))