15
Fibonacci and Going Beyond Recursion
If there was a Greatest Hits list of popular algorithms, the Fibonacci sequence would be right at the top. It would be the Beatles or the Rolling Stones of its generation. The Fibonacci sequence is a series of numbers in which each number is the sum of the previous two numbers. The sequence starts with 0 and 1, and then each subsequent number is the sum of the previous two. So, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.
To dive into that a bit deeper, here is how the sequence is calculated:
fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(2) = 1 // Sum of fibonacci(1) + fibonacci(0) fibonacci(3) = 2 // Sum of fibonacci(2) + fibonacci(1) fibonacci(4) = 3 // Sum ...
Get Absolute Beginner's Guide to Algorithms: A Practical Introduction to Data Structures and Algorithms in JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.