Skip to content

Instantly share code, notes, and snippets.

@film42
Last active December 24, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save film42/6772847 to your computer and use it in GitHub Desktop.
Save film42/6772847 to your computer and use it in GitHub Desktop.
Solution to Project Euler problem 30. Just trying to hack something with Clojure.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Project Euler Problem 30 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Perform n^p
(defn pow [n p]
(reduce * (repeat p n)))
;; Convert a number to a set
(defn number-to-set [n]
(->>
(clojure.string/split (str n) #"")
(rest)
(map read-string)))
;; Sum a (1 2 3), n = 123 to power of p
(defn sum-of-powers [s p]
(->>
(map (fn [n] (pow n p)) s)
(reduce +)))
;; This isn't functional or good.
(def total-sum 0)
;; Euler problem
(defn euler-30 []
(doseq [i (range 10 200000)]
(let [s (number-to-set i)]
(if (= i (sum-of-powers s 5))
(def total-sum (+ i total-sum))))))
;; Main
(defn main []
(euler-30)
(println total-sum))
;; Start
(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment