# Fibonacci numbers with awk

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
only take the first field on each row to compute the fibo on this number
Sample Output
```finish input with:
999
0
1
2
3
eof
26863810024485337815052569207027905710267776959038865271418527606059667101451514099303873236210167827650084004405807693119964697202348044908203804335127173454055409270617765081794282450393830849992803129229312
1
1
1
2
```

-5
2010-11-26 08:36:30

## 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
• recursive version, "pure" AWK Show Sample Output

5
awk 'func f(n){return(n<2?n:f(n-1)+f(n-2))}BEGIN{while(a<24){print f(a++)}}'
· 2010-11-24 10:40:08
• 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

### 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.