Jan 24, 2008

The one line Ruby program for Fibonacci numbers.

Let's write one line Ruby program for Fibonacci numbers.

This is the result.

1
1
2
3
5
8
13
21
34
55

This is the one line program.

lambda{|a,b|10.times{p b=(a=a+b)-b}}.call(1,0)


Smart?

1 comment:

hemanth.hm said...

Set of other codes
Python :
print reduce (lambda x,y:(x[1],x[0]+x[1],x[2]+[x[1]]), xrange(10), (0,1,[]))[2]

Ruby:
ruby -e ‘p,c=0,1;20.times{p p;c=p+p=c}’