javascript fibonaci code example
Example 1: fibonacci javascript
function Fibonacci(num){
var before = 0;
var actual = 1;
var next = 1;
for(let i = 0; i < num; i++){
console.log(next)
before = actual + next;
actual = next
next = before
}
}
Fibonacci(100);
Example 2: javascript fibonacci
function fibonacci(n) {
return n < 1 ? 0
: n <= 2 ? 1
: fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(4));
Example 3: fibonacci javascript
function Fibonacci(valor){
var anterior = 0;
var atual = 1;
var proximo = 1;
for(let i = 0; i < valor; i++){
console.log(proximo)
anterior = atual + proximo;
atual = proximo
proximo = anterior
}
}
Fibonacci(100);
Example 4: fibonacci best performance javascript
function fibonacci(n) {
const list = [0, 1];
for (let x = 2; x < n + 1; x += 1) {
list.push(list[x - 2] + list[x - 1]);
}
return list[n];
}
console.log(fibonacci(4));
Example 5: fibonacci javascript
function fibonacci(num){
var num1=0;
var num2=1;
var sum;
var i=0;
for (i = 0; i < num; i++){
sum=num1+num2;
num1=num2;
num2=sum;
}
return num2;
}
Example 6: fibonacci sums javascript
function fibsSum(n) {
if ( n === 1 ) {
return 1;
}
if (n === 2 ) {
return 2;
}
let sum = fibsSum(n-1) + n;
return sum;
}