Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn collatz-next
- "Computes the next number of the Collatz conjecture series."
- [a]
- (/ (if (even? a)
- a
- (inc (* 3 a )))
- 2))
- (defn collatz-length
- "Computes how long is Collatz conjecture series for particular n."
- [n]
- (loop [number n length 0]
- (if (= number 1)
- length
- (recur (collatz-next number) (inc length)))))
- (defn longest-collatz
- [n]
- (loop [i 1 best '(1 0)]
- (let [length (collatz-length i)]
- (if (< i n)
- (if (> length (second best))
- (recur (inc i) (list i length))
- (recur (inc i) best))
- best))))
- (time (longest-collatz 1000000))
Advertisement
Add Comment
Please, Sign In to add comment