Skip to content

Instantly share code, notes, and snippets.

@harvimt
Created May 22, 2011 23:07
Show Gist options
  • Save harvimt/985991 to your computer and use it in GitHub Desktop.
Save harvimt/985991 to your computer and use it in GitHub Desktop.
Project Euler Problem 2 in Haskell Recursive Implementation and List Comprehensions, In Python Imperitive implementation
--Haskell using recursion
problem2_fp = fibblte_h 4000000 1 1 0
where fibblte_h x y z s
| y + z >= x = ns
| otherwise = fibblte_h x z (y + z) ns
where ns
| even (y + z) = s + y + z
| otherwise = s
# Python Imperative Implementation
def problem2_imp():
sum = 0
current = prev = 1
four_million = 4e6
while current <= four_million:
newcurrent = prev + current;
prev = current;
current = newcurrent;
if newcurrent % 2 == 0:
sum += newcurrent
return sum
-- Haskell using List Comprehensions
fibb 0 = 1
fibb 1 = 1
fibb x = (fibb (x - 2) ) + (fibb (x - 1))
problem2_lc = sum ( takeWhile (<=4000000) [ fibb x | x <- [1..], even (fibb x) ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment