Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add solution to problem 14.

  • Loading branch information...
commit b6506e56904c7a59e520c08055b7412cf6fed838 1 parent 62a0594
@smerrill authored
Showing with 23 additions and 0 deletions.
  1. +23 −0 src/project_euler_clojure/problem14.clj
View
23 src/project_euler_clojure/problem14.clj
@@ -0,0 +1,23 @@
+(ns project-euler-clojure.core)
+
+(comment Which starting number, under one million, produces the longest chain?)
+
+(defn sequence-step [n]
+ (if
+ (= n 1) 0
+ (if
+ (even? n)
+ (/ n 2)
+ (inc (* 3 n)))))
+
+(defn iterative-sequence [n]
+ (when (> n 0)
+ (lazy-seq (cons n (iterative-sequence (sequence-step n))))))
+
+(def e
+ (zipmap
+ (range 1 1000000)
+ (map count (map (-> iterative-sequence) (range 1 1000000)))))
+
+(apply format "The number %d made a chain %d long."
+ (last (sort-by last e)))
Please sign in to comment.
Something went wrong with that request. Please try again.