Skip to content

Instantly share code, notes, and snippets.

@kaz-yos
Last active August 29, 2015 14:12
Show Gist options
  • Save kaz-yos/da7e2b178723f12a321a to your computer and use it in GitHub Desktop.
Save kaz-yos/da7e2b178723f12a321a to your computer and use it in GitHub Desktop.
Happy New Year 2015
;; https://sites.google.com/site/unclebobconsultingllc/home/articles/clojure-prime-factors
(defn factors [n]
(let [candidates (range 1 (inc n))]
(filter #(zero? (rem n %)) candidates)))
(defn prime? [n]
(or (= n 2)
(= 2 (count (take 3 (factors n))))))
(defn prime-factors [n]
(filter prime? (factors n)))
;; Get prime factors for number 2015
(prime-factors 2015)
;; => (5 13 31)
;; Using only these factors (5 13 31), construct numbers
[(- 13 5) (- 31 (* 5 5) 5) (+ 13 (- 13 5 5)) (+ 13 (- 13 5 5)) (* 5 5) (- (- 31 (* 5 5) 5) (* 13 5)) (+ 13 (- 31 (* 5 5) 5)) 5 (+ 13 5 5) (- (- 31 (* 5 5) 5) (* 13 5)) (* 5 5) 5 (- 31 (* 5 5) 5) (+ 13 5)]
;; => [8 1 16 16 25 -64 14 5 23 -64 25 5 1 18]
;; Get 96
(+ 31 (* 5 13))
;; => 96
;; Some manipulations
(->> [8 1 16 16 25 -64 14 5 23 -64 25 5 1 18]
(map #(+ 96 %), )
(map char, )
(map str, )
(apply str, ))
;; => "happy new year"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment