Skip to content

Instantly share code, notes, and snippets.

@davidawad
Last active August 29, 2015 14:05
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 davidawad/674a813323c9bfa51f50 to your computer and use it in GitHub Desktop.
Save davidawad/674a813323c9bfa51f50 to your computer and use it in GitHub Desktop.
Solution to Project Euler #2 on Hackerrank

#A solution to the Project Euler+ problem #2 on Hackerrank

This one uses Memoization so it's as efficient as possible. Unless it could be better! let me know!

## Example 5: Using memoization as decorator
class Memoize:
def __init__(self, fn):
self.fn = fn
self.memo = {}
def __call__(self, arg):
if arg not in self.memo:
self.memo[arg] = self.fn(arg)
return self.memo[arg]
##@Memoize
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a
buff = int(input())
for i in range(0,buff):
curr = int(input())
j=0
sum=0
while fib(j) < curr :
if fib(j)%2 == 0:
sum+=fib(j)
j+=1
print (str(sum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment