Skip to content

Instantly share code, notes, and snippets.

@Nican
Created January 10, 2012 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nican/1591641 to your computer and use it in GitHub Desktop.
Save Nican/1591641 to your computer and use it in GitHub Desktop.
Find even fibonacci numbers
;;Returns the next even fibbonaci number
;; F[n] = 3*F[n-1] + 4*F[n-2] + 5F[n-3] + ... + (x+2)*F[n-x]
(defn evenfib-next [seq]
(letfn getsum [seq count]
(cond
(empty? seq) 0
:else (+
(* count (first seq))
(getsum (rest seq) (+ count 1)))))
(cons (getsum seq 3) seq)
)
;Output: ([8 2] (32 8 2) (138 32 8 2) (594 138 32 8 2) (2556 594 138 32 8 2) (10998 2556 594 138 32 8 2))
; 8 and 2 are the first 2 even fibbo numebers
(print (take 6 (iterate evenfib-next [8 2])))
;Want: (10998 2556 594 138 32 8 2)
;Instead of creating a list of lists, some kind of function that just appends the list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment