Public
Edited
Jun 11, 2024
1 fork
Insert cell
Insert cell
function fib(n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
Insert cell
fib(0)
Insert cell
fib(1)
Insert cell
fib(2)
Insert cell
fib(3)
Insert cell
fib(4)
Insert cell
fib(5)
Insert cell
fib(10)
Insert cell
fib(10)
Insert cell
fib(20)
Insert cell
fib(30)
Insert cell
fib(40)
Insert cell
Insert cell
function fibM(n) {
let m = { 0: 0, 1: 1 };

function add(n) {
if (n in m) return m[n];
m[n] = add(n - 1) + add(n - 2);
return m[n];
}

return add(n);
}
Insert cell
fibM(50)
Insert cell
fibM(100)
Insert cell
fibM(200)
Insert cell
function fibN(n) {
let m = { 0: 0n, 1: 1n };

function add(n) {
if (n in m) return m[n];
m[n] = add(n - 1) + add(n - 2);
return m[n];
}

return add(n);
}

Insert cell
fibN(50)
Insert cell
fibN(100)
Insert cell
fibN(200)
Insert cell
fibN(2000)
Insert cell
fibN(3000)
Insert cell
fibN(4000)
Insert cell
fibN(5000)
Insert cell
fibN(6000)
Insert cell
function fibL(n) {
let adds = [0n, 1n];
for (let i = 2; i <= n; i++) {
const res = adds[i - 1] + adds[i - 2];
adds.push(res);
}
return adds.at(-1);
}

Insert cell
fibL(7000)
Insert cell
function fibI(n) {
let curr = 1n, prev = 0n, next;
for (let i = 2; i <= n; i++) {
next = curr + prev;
prev = curr
curr = next
}
return next;
}

Insert cell
fibI(7000)
Insert cell
function fibP(n) {
let adds = [0, 1];
for (let i = 2; i <= n; i++) {
const res = adds[i - 1] + adds[i - 2];
adds.push(res);
}
return adds;
}

Insert cell
fibP(102)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more