How to resolve the algorithm Fibonacci sequence step by step in the sed programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fibonacci sequence step by step in the sed programming language
Table of Contents
Problem Statement
The Fibonacci sequence is a sequence Fn of natural numbers defined recursively:
Write a function to generate the nth Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion). The sequence is sometimes extended into negative numbers by using a straightforward inverse of the positive definition: support for negative n in the solution is optional.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fibonacci sequence step by step in the sed programming language
Source code in the sed programming language
#!/bin/sed -f
# First we need to convert each number into the right number of ticks
# Start by marking digits
s/[0-9]/<&/g
# We have to do the digits manually.
s/0//g; s/1/|/g; s/2/||/g; s/3/|||/g; s/4/||||/g; s/5/|||||/g
s/6/||||||/g; s/7/|||||||/g; s/8/||||||||/g; s/9/|||||||||/g
# Multiply by ten for each digit from the front.
:tens
s/|<||||||||||/g
t tens
# Done with digit markers
s//g
# Now the actual work.
:split
# Convert each stretch of n >= 2 ticks into two of n-1, with a mark between
s/|\(|\+\)/\1-\1/g
# Convert the previous mark and the first tick after it to a different mark
# giving us n-1+n-2 marks.
s/-|/+/g
# Jump back unless we're done.
t split
# Get rid of the pluses, we're done with them.
s/+//g
# Convert back to digits
:back
s/||||||||||/
s/<\([0-9]*\)$/<0\1/g
s/|||||||||/9/g;
s/|||||||||/9/g; s/||||||||/8/g; s/|||||||/7/g; s/||||||/6/g;
s/|||||/5/g; s/||||/4/g; s/|||/3/g; s/||/2/g; s/|/1/g;
s/|/g
t back
s/^$/0/
You may also check:How to resolve the algorithm Old Russian measure of length step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Lua programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the Fortran programming language
You may also check:How to resolve the algorithm Sockets step by step in the C programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the BBC BASIC programming language