fib=[] def fibo(a=-1,b=1,upto=4000000): if a+b>=upto: return else: a,b=b,a+b fib.append(b) fibo(a,b) fibo() even=[i for i in fib if not i%2] print sum(even)

## Monday, February 23, 2009

### Fibonacii series

I absolutely love this program I wrote to solve the Project Euler 2 nd problem

### The Pythonic way!

With my earlier experience of Project Euler (I used to solve in Java), after being quite well conversant in Python, I just had a re-look. I was simply pleased by the compactness, doing it in the pythonic way achieved.
The question is to find the sum of all numbers between 1 and 1000 that are divisible by either 3 or 5.
Here is how simply, we can do it in python:

a=[i for i in xrange(1000) if not (i%3 and i%5)] print sum(a)

Subscribe to:
Posts (Atom)

## Blog Archive

## Labels

Python
(3)
Programming
(2)
Project Euler
(2)
Java
(1)
Math
(1)
idea
(1)
presentation
(1)
pythonic
(1)
stackoverflow
(1)
topcoder
(1)