|
Fibonacci Series Printer: msg#00079editors.sed.user
Happy New Year, Sedders! I wrote a script to print Fibonacci Series (for fun). It is very limit on the length that it can print. And there must be places to be improved in this script. So you may help to improve it, do some comment on it or give your own approach. ===== filename: fib.sed ===== start # Fibonacci Series Printer # To know more on how the numbers are represented by # letters here, you should read Greg Ubben's tutorials # on word counting and adding numbers which you can # find here: # http://sed.sourceforge.net/grabbag/tutorials/ # Initialize the series to "1,1" # gfedcbaa=1 gfedcbaaa=2 ... gfedccbaa=101 1{s/.*/ mlkjihgfedcbaa mlkjihgfedcbaa /;h;d} # get last two numbers from hold space g s/.* \(.*\) \(.*\) $/\1\2/ # and sort them so that we can do carray operation :lpm s/m\([^m].*\)$/\1m/ tlpm :lpl s/l\([^l].*\)$/\1l/ tlpl :lpk s/k\([^k].*\)$/\1k/ tlpk :lpj s/j\([^j].*\)$/\1j/ tlpj :lpi s/i\([^i].*\)$/\1i/ tlpi :lph s/h\([^h].*\)$/\1h/ tlph :lpg s/g\([^g].*\)$/\1g/ tlpg :lpf s/f\([^f].*\)$/\1f/ tlpf :lpe s/e\([^e].*\)$/\1e/ tlpe :lpd s/d\([^d].*\)$/\1d/ tlpd :lpc s/c\([^c].*\)$/\1c/ tlpc :lpb s/b\([^b].*\)$/\1b/ tlpb :lpa s/a\([^a].*\)$/\1a/ tlpa # do some adjustments # in analog add we have : # edcbaa + edcbaaa = edcbaaaa # 1 + 2 = 3 # but we get "eeddccbbaaaaa" after sorting # so adjustment is needed s/\(.\)\(\1\{1,19\}\)/\2/g # do carry operation # change "baaaaaaaaaaa" into "bba" :doCarry s/\(.\)\(\(.\)\3\{9\}\)\3/\1\1\3/ t doCarry # a loose check on overflow. /^\(.\)\1\{9\}/{g;s/$/ ++OVERFLOW++/; b2digi} # save to holdspace and add a SPACE at the end G s/\(.*\)\n\(.*\)/\2\1 / $!{h;d} g # translate analog numbers to digits :2digi s/\([a-z]\)\1\{9\}/9/g s/\([a-z]\)\1\{8\}/8/g s/\([a-z]\)\1\{7\}/7/g s/\([a-z]\)\1\{6\}/6/g s/\([a-z]\)\1\{5\}/5/g s/\([a-z]\)\1\{4\}/4/g s/\([a-z]\)\1\{3\}/3/g s/\([a-z]\)\1\1/2/g s/\([a-z]\)\1/1/g s/\([a-z]\)/0/g s/ 0*/ /g # q command is necessary for overflow check q ===== end For sed needs input you can use it this way: seq 10|sed -f fib.sed to print the first ten Fib numbers. or: yes|sed -f fib.sed to print until it meets an overflow. -- Regard, hq00e Sun Jan 1 00:13:03 China Standard Time 2006 ------------------------ Yahoo! Groups Sponsor --------------------~--> Most low income homes are not online. Make a difference this holiday season! http://us.click.yahoo.com/5UeCyC/BWHMAA/TtwFAA/dkFolB/TM --------------------------------------------------------------------~-> -- Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> To unsubscribe from this group, send an email to: sed-users-unsubscribe@xxxxxxxxxxxxxxx <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/ |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: sed one-liner?: 00079, Eric Pement |
|---|---|
| Next by Date: | Re: Fibonacci Series Printer: 00079, hq00e |
| Previous by Thread: | Please submit changes, new material for sed FAQi: 00079, Eric Pement |
| Next by Thread: | Re: Fibonacci Series Printer: 00079, hq00e |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |