Giter Site home page Giter Site logo

fibonacci-numbers's Introduction

Fibonacci numbers

https://www.geeksforgeeks.org/wp-content/uploads/fibonacci-sequence.png

Write a function that generates and returns the n-th fibonacci number.

From Wikipedia:

In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones.

These are the first 8 fibonacci numbers:

1, 1, 2, 3, 5, 8, 13, 21 ...

Therefore fibonacci(8) should dynamically calculate and return the value 21.

Further - optionally return a list

Modify your algorithm so that it takes in a second parameter, returnList. When the value is true, return an array of the fibonacci numbers up to the n-th number. When false, return the n-th number itself.

The new function signature should be:

function fibonacci(n, returnList) {
    // ...
}

Further - recursive algorithm

Every algorithm can be implemented iteratively and recursively. One approach may be more suitable than the other in most situations, but it's still feasible to implement both.

In the case of Fibonacci, a recursive algorithm might be easier to reason about and read. Here is a naive recrusive implementation of Fibonacci:

function fibonacci(n) {
    if (n === 1 || n === 2) return 1;

    return fibonacci(n - 2) + fibonacci(n - 1);
}

Yes, you're not seeing things - this function is calling itself. Some might say, it's recursing on itself. This is perfectly valid code.

Play with the above function by adding console.log and debugger statements and try and understand how it works. Then, delete the code and try implementing it yourself.

Further - visualize

Use HTML5 canvas to draw the above figure (without the numbers). If you are able to draw the squares, try adding a random color to each new square.

Further - dynamic programming

An algorithm is of no practical use if it runs too slowly. Try computing fibonacci(100) with the above naive recursive implementation.

Still waiting? It will take a while. That's because each time the function is called, it is called 2 more times, and then each of those calls the function twice more, and so on. It is doing a lot of computation!

So we would call this particular algorithm (in its current form) a naive algorithm. Why? Because it brute forces its way to the result, re-running the exact computation multiple times. It is extremely inefficient, and takes a long time to complete.

One common way to deal with such problems is trading space for time. Specifically, we can use a concept called dynamic programming to optimise our algorithm. From Wikipedia:

A dynamic programming algorithm will examine the previously solved subproblems and will combine their solutions to give the best solution for the given problem.

Implementing this optimised algorithm is obviously far beyond the limits of this morning exercise.

After class, try googling for "dynamic programming" and try and implement a recursive algorithm that uses dynamic programming to execute fibonacci(100) in under 1 second.

This, as you might imagine, is a commonly asked question by technical interviewers.

Good luck!

fibonacci-numbers's People

Contributors

nickangtc avatar awongh avatar ckkok avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.