# Fibonacci numbers with awk

awk 'func f(n){return(n<2?n:f(n-1)+f(n-2))}BEGIN{while(a<24){print f(a++)}}'
recursive version, "pure" AWK
Sample Output
```0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657```

5
2010-11-24 10:40:08

## 4 Alternatives + Submit Alt

• Another combination of seq and awk. Not very efficient, but sufficiently quick. Show Sample Output

14
seq 50| awk 'BEGIN {a=1; b=1} {print a; c=a+b; a=b; b=c}'
· 2009-03-24 20:39:24
• Does not require input to function or complete. Number of iterations controlled by shell variable \$NUM. Show Sample Output

0
awk 'BEGIN {a=1;b=1;for(i=0;i<'\${NUM}';i++){print a;c=a+b;a=b;b=c}}'
· 2009-09-06 03:05:55

• 0
prev=0;next=1;echo \$prev;while(true);do echo \$next;sum=\$((\$prev+\$next));prev=\$next;next=\$sum;sleep 1;done
· 2009-09-06 09:38:10
• only take the first field on each row to compute the fibo on this number Show Sample Output

-5
gawk '{n=\$1;a=0;b=1;c=1;for(i=1;i<n;i++){c=a+b;a=b;b=c};print c}' << eof
· 2010-11-26 08:36:30

### What Others Think

Recursion: Pretty much always the wrong way to do something. Unless you're using a compiler on it before you run it, it will almost certainly be massively slower than a loop that does the same thing. Even with a compiler it's not a certainty you'll achieve comparable speeds. This, for instance, is 30x slower than http://www.commandlinefu.com/commands/view/1654/fibonacci-numbers-with-awk
TheMightyBuzzard · 720 weeks and 1 day ago
who cares? It's elegant. If you really need speed you know where to find a C compiler ;-)
unefunge · 720 weeks and 1 day ago
Info buat kalian yang mau berjudi slot online. Bila anda merupakan salah satu orang penggemar slot, sebaiknya mulai sekarang anda bermain lah di situs SENSA838 . Tentu saja ada alasan mengapa hal tersebut disarankan. Bermain di slot online itu gampang, dan jelas aman bro. anda tak harus lagi deg deg ser jantungnya takut ada penggerebekan. Tentu saja pilihan agen slot nya pun banyak dan anda bisa bermain dengan sangat nyaman pada rtp live dan provider slot yang lainnya. Tentu saja bila anda ingin nyaman taruhan, salah satu hal yang perlu anda lakukan adalah dapatkan info RTP live yang valid. Dapatkan info terkini sehingga anda bisa bermain dengan tepat. Kalian dipastikan akan sangat nyaman bila ada di dunia maya untuk mainkan slot. Anda pasti juga akan sangat mungkin bermain slot sebab sudah tak ada lagi aturan deposit besar
missmawar222 · 81 weeks and 4 days ago
It is nevertheless needed to mark similarly, that the folded structure of organization largely stipulates creation of ideas on an exit from the folded situation. https://zvarka-info.8b.io/
JosephWinner23 · 25 weeks ago
With Carbide End Milling Cutters TNMG Inserts over 10 years of experience and knowledge, we provide total cutting tool https://derekleopo.exblog.jp/ solutions. Complying to https://billrita.exblog.jp/ international standards, we design Korloy Inserts and develop the best quality and cost-effective https://leandervic.exblog.jp/ cermet inserts SNMX Inserts solutions for PVD Coated Insert our customers. We cermet inserts are produced based on extensive knowledge and competencies.
iscarbide · 11 weeks and 4 days ago

### What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.