Permalink
Browse files

Some improvements on the code.

  • Loading branch information...
1 parent 6f18ebc commit a23542af55c158c5990d8260556db92e70f06b83 @danielfm committed Sep 5, 2009
Showing with 5 additions and 6 deletions.
  1. +1 −1 euler/problem_10.clj
  2. +4 −5 euler/problem_14.clj
@@ -3,7 +3,7 @@
(defn solve []
"Finds the sum of all the primes below two million."
- (reduce + (take-while #(< % 2000000) (filter prime? (iterate inc 1)))))
+ (reduce + (take-while #(< % (int 2e6)) (filter prime? (iterate inc 1)))))
; Expected result: 142913828922
(println (solve))
@@ -1,7 +1,6 @@
(ns euler.problem-14)
-; A smaller and beautiful solution takes ~1.2 min to run, but this
-; one takes only ~15 sec
+;; Odd numbers tends to produce longer chains than even numbers
(defn collatz-count [n]
"Returns the sequence count starting from the natural number n."
@@ -16,13 +15,13 @@
(defn solve []
"Returns the starting number, under one million, which produces the longest chain."
- (loop [n (int 1e6) m 1 lc 0]
+ (loop [n 999999 m 1 lc 0]
(let [c (collatz-count n)]
(if (= n 1)
m
(if (> c lc)
- (recur (dec n) n c)
- (recur (dec n) m lc))))))
+ (recur (- n 2) n c)
+ (recur (- n 2) m lc))))))
; Expected result: 837799
(println (solve))

0 comments on commit a23542a

Please sign in to comment.