Tuesday, 28 June 2011

Problem 2

For project Euler problem - 2  one python solution is given here:
https://github.com/sanand0/euler/blob/master/002.py

The meaning of dict.get is given here:
http://www.java2s.com/Code/Python/Dictionary/Dictionarygetvalue.htm

Consider the statement
cache[n] = cache.get(n, 0) or (n <= 1 and 1 or fib(n-1) + fib(n-2))

or AND and operators are short circuited.
If n <= 1, "n <= 1 and 1" evaluates to True.
so cache[-1],cache[0],cache[-2] evaluates to True.




No comments:

Post a Comment