Can python find fibonacci series in a single line of code?

Bernhard Herzog bh at intevation.de
Tue Nov 12 06:46:08 EST 2002


Fernando Pérez <fperez528 at yahoo.com> writes:

> logistix wrote:
> 
> > python  -c "a,b=1,1;exec('while 1:\n\tglobal a,b\n\tprint
> > a\n\ta,b=b,a+b\n\n')"
> 
> The globals aren't needed, exec() defaults to your current namespace:
> 
> python -c "a,b=1,1;exec('while 1:\n\tprint a\n\ta,b=b,a+b\n')"

The newlines and tabs aren't needed either :)

python -c "a,b=1,1;exec('while 1: print a; a,b=b,a+b')"

Next we get rid of that unnecessary multiple assignment at the beginning
and use long ints to avoid the overflow error:

python -c "a = b = 1L; exec('while 1: print a; a,b=b,a+b')"

It would be nice to get rid of that exec, too...

  Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list