Fibonacci Number Calculation Algorithm
This JavaScript code is written to demonstrate how to calculate the fibonacci numbers. For more information on fibonacci numbers, checkout this post
let data = {}
function calculateFibonacci(num){
if(num <= 2){
return 1;
}
if(data[num]){
return data[num];
}
fibNum = calculateFibonacci(num - 1) + calculateFibonacci(num - 2);
data[num] = fibNum;
return fibNum;
}
Fibonacci Numbers
A fibonacci number is a number that is equal to the sum of the previous two fibonacci numbers. As an example, the first 10 fibonacci numbers are
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Fibonacci numbers get very big, very quickly. The 100th fibonacci number is
354224848179262000000
The 1000th fibonacci number is.
4.346655768693743e+208
Ruby Flatten
Write a summary describing how the method you chose works, and give a brief example of how to use it
The .flatten
method provides an easy way to convert multi-dimensional arrays to one-dimensional array. Here is a basic example.
arr = [[1, 2, 3, 4], [5, 6, 7]]
arr.flatten #=> [1, 2, 3, 4, 5, 6, 7]
The .flatten
method works recursively through the array until the array is no longer multi-dimensional
. This behavior can be modified through an optional argument. By passing in a numerical level
parameter, .flatten
will be told to stop after a certain number of levels of recursion.
In the following example, the array arr
goes four levels deep. Note how the increase in the number passed to flatten
changes the output.
arr = [[["a", "b", "c", "d"]], [[["e", "f", "g", "h"]], "j", "k"]]
arr.flatten(1) #=>[["a", "b", "c", "d"], [["e", "f", "g", "h"]], "j", "k"]
arr.flatten(2) #=>["a", "b", "c", "d", ["e", "f", "g", "h"], "j", "k"]
arr.flatten(3) #=>["a", "b", "c", "d", "e", "f", "g", "h", "j", "k"]