Search This Blog

2024/03/30

Javascript Algorithm:Fibonacci Sequence

Fibonacci Sequence:
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.the sequence is as below

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,..

How to generate fibonanci Sequence in Javascript:

Each function below prints all fibonancci numbers less than 10.

Solution 1:
function fibonacciSequence(max) {
let startPoint = 0;
let current = 1;
let arr = [0, 1];

let next = startPoint + current;
while (next < max) {
arr.push(next);
startPoint = current;
current = next;
next = startPoint + current;
}
return arr;
}

var resultArray = fibonacciSequence(10);
console.log(resultArray);

Output:
[
0, 1, 1, 2,
3, 5, 8
]

Solution 2:

function fibonacciSequence(max) {
let startPoint = 0;
let current = 1;

console.log(startPoint)
console.log(current)

let next = startPoint + current;
while (next < max) {
console.log(next)
startPoint = current;
current = next;
next = startPoint + current;
}
}

fibonacciSequence(10);

Output:
0
1
1
2
3
5
8


Solution2: Using generator function.

function* fibonacciSequenceGenerator(max) {
let startPoint = 0;
let current = 1;

yield startPoint
yield current

let next = startPoint + current;
while (next < max) {
yield next
startPoint = current;
current = next;
next = startPoint + current;
}
}

let iterator = fibonacciSequenceGenerator(10)
for(let val of iterator){
console.log(val)
}

Output:
0
1
1
2
3
5
8


Solution3:Using Recursion.

function fibonacciSequenceRecursive(index) {
switch (index) {
case 0:
return 0;
case 1:
case 2:
return 1;
default:
return (
fibonacciSequenceRecursive(index - 1) +
fibonacciSequenceRecursive(index - 2)
);
}
}

for (let i = 0; fibonacciSequenceRecursive(i) < 10; i++) {
let si = fibonacciSequenceRecursive(i);
console.log(si);
}

Output:
0
1
1
2
3
5
8


Solution 4: Using generator function
function* fibonacciSequenceGenerator(max) {
let result = 0;
let index = 1;

let obj = {};

while (result < max) {
yield result;
if (index == 1 || index == 2) {
result = 1;
obj[index] = result;
} else {
result = obj[index - 1] + obj[index - 2];
obj[index] = result;
}
index++;
}
}

let iterator = fibonacciSequenceGenerator(10);
for (let val of iterator) {
console.log(val);
}

Output:
0
1
1
2
3
5
8

Solution 5: Using two generator functions:
function* fibonacciGenerator() {
let prev = 0;
let curr = 1;

while (true) {
yield curr;
[prev, curr] = [curr, prev + curr];
}
}

function* firstNFibonacci(max) {
const fibonacci = fibonacciGenerator();
let result = fibonacci.next().value;

while (result < max) {
yield result;
result = fibonacci.next().value;
}
}

// Create a generator instance for the first 3 Fibonacci numbers
const iterator = firstNFibonacci(10);

// Generate and print the first 3 Fibonacci numbers
for (const val of iterator) {
console.log(val);
}

Output:
0
1
1
2
3
5
8

No comments:

Post a Comment