Created
May 22, 2011 23:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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