Skip to content

Instantly share code, notes, and snippets.

@ccunning2
Last active April 1, 2020 00:56
Show Gist options
  • Save ccunning2/8e1537697c386a40830d1cd3e0f05ce9 to your computer and use it in GitHub Desktop.
Save ccunning2/8e1537697c386a40830d1cd3e0f05ce9 to your computer and use it in GitHub Desktop.
Clojure Solution
; This function will generate a fibonacci sequence of length n
(defn createFib
"Usage: (createFib n), creates a fibonacci sequence of length n"
([length] (createFib length [0 1]))
([length a] (if (= (count a) length) a (recur length (conj a (+' (peek a) (peek (pop a)) ))) )) )
;Notes: The above function is recursive, with multiple 'arity', meaning that it has a definition for multiple numbers of parameters.
;If you call it with one argument (as you are intended to), then it calls the itself with the two beginning numbers of
;the fib sequence, 0 and 1. From here, it recursively builds the list, by (conj)ugating the input list with
; (peek a) which is the last value in the list and (peek (pop a) ), which is the second to last value in the list.
;Hopefully that makes sense!
;Now that we have a function to give us a fib sequence, lets filter out only the even values
;from the fib sequence of length 4000
(def filteredList (filter #(= 0 (mod % 2)) (createFib 4000)))
;So in other words, give me the list where the numbers mod 2 = 0, AKA even numbers.
;Finally, give me the sum
(reduce + filteredList)
;;The answer I get is
;32287442245474086765688474507684797822206950320075671354203788799088605179517044457224738903643620871880370761891909448749613504871091576241009531381775399371852137553428235108153796811528694253388383601034835238753044447647150254645583011973933420881926976906991140851968332684961354547654003410699762390360524977914595703514971811043889648229587006305074329760190585226295570665974668040288570854322891803318040970957608677557905496986972891746991922296374836330680774030807878297909472158809961048684958838487029103170946044072274668987211476070066310784170350508136711363913881363076533151546526491025878722371214016553761209733109827890206550879752615808611289146243040501195609392594649837878883460134701174366822331362887358870462034414150093219712960880541272731582314403851326876309808078662217020171028668341639642049295400750
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment