print$}+=$.||1,$.+=$}while.1
Update:
The same in 27 chars:
print$}+=$.=$}-$.||1while.1
| [reply] [d/l] [select] |
it would nice to explain the code ...
Murcia
| [reply] |
print$}+=$.=$}-$.||1while.1
# means -
while ( 1 ) {
$. = $} - $.;
$. ||= 1;
$} = $} + $.;
print $};
}
| [reply] [d/l] |
{print$}+=$.||1,$.+=$};redo}
| [reply] [d/l] |
Please, recheck your code. It should output these numbers:
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 ...
i.e.:
perl -e '$.++;print$}+=$.,q| |,$.+=$},q| |while+1'|more
| [reply] [d/l] |
Which part are you criticizing? I thought fibo numbers where 1,1,2,3,5,8 which would make his correct. Just asking for clarification if there was some other difference i missed.
His outputs 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597 for me.
Update: /me does some googling and finds both of those and 0,1,1,2,3 as pretty evenly distributed....hmm.
| [reply] |
This is even shorter, but it prints newlines, not spaces.
It's better than the perl one because it calculates all digits.
dc -e1d[pdsd+ldrlxx]dsxx
If it's enough to have the first say 40 elements of the sequence (depending on an internal limitation of dc) then this is 2 chars shorter:
dc -e1d[pdk+Krlxx]dsxx
If you insist on spaces, here's a version:
dc -e1d[ddn32Psd+ldrlxx]dsxx
These depend on the dc extension command r (and the last one on n and P too),
but these are present in both GNU dc and FreeBSD dc, so it might be portable now.
Update 2006 nov 15:
This is slightly longer than the above but is still interesting:
dc -e'1[pdd5**v1++2/lxx]dsxx'
Update 2007 jul 17: some explanation in
Re^3: Fibo Golf challenge on 3 monkeys.
| [reply] [d/l] [select] |
Excellent ++ very succinct! | [reply] |