Last active
December 24, 2015 08:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; 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