Clojure Euler: Problem 025

What is the first term in the Fibonacci sequence to contain 1000 digits?


We’ve introduced Fibonacci sequence at Clojure Euler: Problem 002

Lazy sequence generates fibonacci numbers is dead simple:

(defn fibonacci []
  (->> [0 1]
       (iterate (fn [[a b]] [b (+' a b)]))
       (map first)))

We also can count the length of number in digits. No decimal arithmetic. Cast number to string, and take length of a string

(defn num-of-digits [n]
  (count (str n)))

To solve the problem use the most straightforward approach and you’ll find the answer in less than 1 second.

(->> (fibonacci)
     (map-indexed (fn [i n] [i (num-of-digits n)]))
     (drop-while #(< (second %) 1000))


P.S. If you followed all posts upto this one, then you have 25 problems solved, level 1 and nice badge

mishadoff 09 September 2014
blog comments powered by Disqus