# The art of problem-solving using the Fibonnaci sequence challenge

On the basic level, every line of code written exists as part of a solution to the problem the author is trying to solve. Programming is an art, and like every good work of art, it takes careful planning and deliberate thoughtfulness to craft a beautiful solution to any problem.

This article aims to put a name to the step taken to solve a coding challenge. And these ideas and methods can be employed in various other fields where efficient solutions to challenges are needed.

### The Fibonacci sequence challenge

Write a function (*fib_string*) that takes a number * n* and prints the Fibonacci sequence

*times.*

`n`

Constraints:

`n = int`

`0 ≤ n ≤ 10⁴`

**Example**

Sample input => `4`

Sample Output => `0 1 1 2`

### Solving the problem

- Understanding the problem
- Develop an algorithm
- Write the code and optimize it.

**Understanding the problem**
To understand the problem that is to be solved, there is a need to read the problem statement, then two things will follow:

- Ask questions.
- Analyze the sample given.

For the challenge being considered in this post, the main question that comes to mind is

- What is a Fibonacci sequence?
The Fibonacci sequence is a series of numbers where a number is the addition of the last two numbers, starting with 0, and 1. eLearningindustry.com

Once the definitions are out of the way, you'd want to analyze the sample given to get a view of what the output should look like.

From the example, it can be deduced that depending on the `n`

value supplied, the output is to return the first `n`

Fibonacci sequence, therefore if `n = 100`

, the function should return the first 100 Fibonacci sequences.

**Develop an algorithm**

An algorithm is a set of finite instructions designed to perform certain tasks - techterms

Using the iterative approach

- Initialize a list holding the first two Fibonacci sequence;
`output = [0, 1]`

- Initialize
`while`

loop in*range[0, n-2]* - Compute next number in series; next = output[i] + output[i + 1]
- Append next value to the
`output`

list - Join the value in the output and return it

**Write the code and optimize it**

This step can be done using any programming language of your choice, I use **Python** in this post.

```
def fib_string(n):
output = [0, 1]
result = " "
if type(n) == str or n > 10000:
return "Enter a number less than 10000"
if n <= 0:
return ""
if n == 1:
result = output[0]
return result
if n == 2:
return result.join(map(str, output))
i = 0
while i < n - 2:
next = output[i] + output[i + 1]
output.append(next)
i += 1
return result.join(map(str, output))
```

Note: The code above is seen (the first two if blocks) to handle the constraints as set out in the problem statement.

If you found this article useful, drop an emoji 😃