Skip to content

Commit

Permalink
complete
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelAtFlatiron committed Jun 18, 2024
1 parent 5dfba2b commit e040a48
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
# Practice Lesson - Algorithmic Problem Solving Framework

## Introduction
## Overview

The Fibonacci sequence is a well known sequence of numbers in mathematics where the current number is the sum of the previous two numbers. While there has been evidence of the understanding of the Fibonacci sequence in Greek and Indian history it has been named after the Italian mathematician Leonardo of Pisa who first introduced the sequence in 1202. Fibonacci numbers are often found in nature - for example the number of petals on flowers, the spiraling patterns in pine cones, pineapples, and cauliflower, and sea shells. As a result the Fibonacci sequence is also prevalent in a lot of artwork. Calculating the Fibonacci sequence is also one of the most famous algorithms that all beginner programmers know.
The Fibonacci sequence is a well known sequence of numbers in mathematics where the current number is the sum of the previous two numbers. While there has been evidence of the understanding of the Fibonacci sequence in Greek and Indian history it has been named after the Italian mathematician Leonardo of Pisa who first introduced the sequence to Western mathematics in 1202. Fibonacci numbers are often found in nature - for example the number of petals on flowers, the spiraling patterns in pine cones, pineapples, and cauliflower, and sea shells. As a result the Fibonacci sequence is also prevalent in a lot of artwork. Calculating the Fibonacci sequence is also one of the most famous algorithms that all beginner programmers know.

## Tools and Resources

[The magic of Fibonacci numbers - TED](https://www.youtube.com/watch?v=SjSHVDfXHQ4)
[How to draw the Fibonaci sequence](https://www.youtube.com/watch?v=qTw_qay54WI)
[The Golden Ratio - PBS](https://www.youtube.com/watch?v=1Jj-sJ78O6M)

## Setup

**Fork and clone** this practice lesson into your local environment. Navigate into its
directory in the terminal, then run `code .` to open the files in Visual Studio
Code.

In the root directory run `npm install`.
[How to draw the Fibonacci sequence](https://www.youtube.com/watch?v=qTw_qay54WI)

Open `src/index.js`. This is where you will be writing your code.

You can run `npm test` to test your code.
[The Golden Ratio - PBS](https://www.youtube.com/watch?v=1Jj-sJ78O6M)

## Instructions

The Fibonacci sequence is the begins as `1, 1, 2, 3, 5, 8, 13, 21, 34, ...`. The current number is the previous two numbers sum. The only exception are the first two numbers of the sequence: 1 and 1 (although some mathematicians may start with 0 and 1).
The Fibonacci sequence begins as `1, 1, 2, 3, 5, 8, 13, 21, 34, ...`. The current number is the sum of the previous two numbers. The only exceptions are the first two numbers of the sequence: 1 and 1 (although some mathematicians may start with 0 and 1).

Your task is to use the algorithmic problem solving framework to create an algorithm to calculate the `nth` number in the sequence. For example:

Expand Down Expand Up @@ -57,18 +47,18 @@ Return the `nth` number of the Fibonacci sequence where the first two numbers ar

**2. Problem Analysis**

We will have to keep track of the previous two numbers and add them together. Once we get the new value we will have to update the previous tow numbers and continue. Once we reach the `nth` iteration we will return that number.
We will have to keep track of the previous two numbers and add them together. Once we get the new value we will have to update the previous two numbers and continue. Once we reach the `nth` iteration we will return that number.

However there are two special cases: our first two numbers. Since there are no previous numbers to together we will have to somehow take into account the `1st` and `2nd` values of the Fibonacci sequence.
However there are two special cases: our first two numbers. Since there are no previous numbers to add together we will have to somehow take into account the `1st` and `2nd` values of the Fibonacci sequence.

**3. Algorithm Development**

We know that `1` and `1` will be our starting point so we will initialize variables that are equal to those numbers.

```
```js
// handle special cases if n is 1 or 2
// create variable for first and second value
// create for loop from 3 (since we already have the first two numbers of the sequence) and iterate `n` number of times
// create for loop starting from 3 (since we already have the first two numbers of the sequence) and iterate `n` number of times
// add up the available numbers
// update the variables to save the new numbers
// return final number
Expand Down Expand Up @@ -101,6 +91,14 @@ function fib(num){

It is always important to test your base cases. Since the math is pretty straightforward it would be nice to test a few random values for `num`.

**6. Maintenance**

This would be a good point to see if you can optimize your code.

**7. Document and Comment**

This would be a good point to simplify your comments.

And there it is. The Fibonacci sequence.


11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

function fib(num){
// Code here
if(num <= 2){
return 1;
}
let prev = 1;
let cur = 1;
for(let i = 3; i <= num; i++){
let newCur = prev + cur;
prev = cur;
cur = newCur;
}
return cur;
}

export { fib }

0 comments on commit e040a48

Please sign in to comment.