3 ms·
For the clojure people: (let [gcd (fn [x y] (if (pos? y) (recur y (mod x y)) x))] (->> [2 7 1] (iterate (fn [[n a]] (let [g (gcd n a)]
by dmichulke 11y ago
For the clojure people:
(let [gcd (fn [x y]
(if (pos? y)
(recur y (mod x y))
x))]
(->> [2 7 1]
(iterate (fn [[n a]]
(let [g (gcd n a)]
[(inc n) (+ a g) g])))
(filter (comp #(< 1 %) last))
(take 20)))
I would format it better if I knew how to.
Anyway, the last element of each vector (that is, the g) in the resulting sequence is always a prime.
- dmichulke 11y agoRegarding run time, you can see there is no free lunch: The first twenty elements are returned in 4msecs. The first 200 are still running (> 2 hours already).