In mathematics, the Fibonacci sequence is a sequence in which each number is the sum of the two preceding ones.
Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers, commonly denoted Fn .
The sequence commonly starts from 0 and 1, although some authors start the sequence from 1 and 1 or sometimes (as did Fibonacci) from 1 and 2.
Starting from 0 and 1, the sequence begins
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ....
The Fibonacci numbers were first described in Indian mathematics as early as 200 BC in work by Pingala on enumerating possible patterns of Sanskrit. They are named after the Italian mathematician Leonardo of Pisa, also known as Fibonacci, who introduced the sequence to Western European mathematics in his 1202 book Liber Abaci
Source Wikipedia
This example calculates the Fibonacci sequence for a given set of numbers. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. In this program, you provide a list of numbers (e.g., [10, 20, 30, 40]) for which you want to calculate the Fibonacci sequence. The program then calculates the Fibonacci sequence for each of these numbers and stores the calculated sequences. The output is an array of arrays, where each inner array represents the Fibonacci sequence for one of the input numbers. You can use the calculated Fibonacci sequences for further analysis, visualization, or any other purpose in your JavaScript program.
const calculateFibonacci = function() {
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
let results = [];
for (let i = 0; i < params.array.length; i++) {
results.push(fibonacci(params.array[i]));
}
rtn.data.push(results);
};
const params = {
array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32],
threads: hamsters.maxThreads,
aggregate: false,
mixedOutput: true
};
hamsters.run(params, calculateFibonacci, function(results) {
console.info(results);
}, function(error) {
console.error(error);
});