Skip to content

Instantly share code, notes, and snippets.

@ranjanprj
Created May 8, 2017 14:27
Show Gist options
  • Save ranjanprj/7cb544392f2cbb6ded9e249825c98d28 to your computer and use it in GitHub Desktop.
Save ranjanprj/7cb544392f2cbb6ded9e249825c98d28 to your computer and use it in GitHub Desktop.
;Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
;1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
;By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
;*** PROBLEM WITH EULER CONDITION THEY SAY 4 MILLION BUT ACTUALLY MEAN 40 MILLION
(defn fibr-sum-even[upto]
(loop [a 0N b 1N sum 0N]
(println (str b " - " sum))
(if (> b upto) sum
(if (= (mod b 2) 0)
(recur b (+ a b) (+ sum b))
(recur b (+ a b) sum )))))
(fibr-sum-even 4000000N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment