Public
Edited
Jun 11, 2024
Insert cell
Insert cell
function fact(n) {
if (n == 1) return 1;
return fact(n - 1) * n;
}
Insert cell
fact(1)
Insert cell
fact(2)
Insert cell
fact(3)
Insert cell
fact(4)
Insert cell
fact(5)
Insert cell
Insert cell
function factI(n) {
let res = 1;

for (let i = 1; i <= n; i++) {
res = res * i;
}

return res;
}
Insert cell
factI(5)
Insert cell
Insert cell
fact(10)
Insert cell
fact(20)
Insert cell
fact(21)
Insert cell
fact(22)
Insert cell
function factN(n) {
if (n == 1) return 1n;
return BigInt(n) * BigInt(fact(n - 1));
}
Insert cell
factN(21)
Insert cell
factN(22)
Insert cell
factN(30)
Insert cell
factN(100)
Insert cell
Insert cell
function pow(base, count) {
function multiply(n) {
if (n == 1) return base;
return base * multiply(n - 1);
}

return multiply(count);
}
Insert cell
pow(2,3)
Insert cell
pow(3,2)
Insert cell
Insert cell
function powS(base, count) {
function multiply(n) {
if (n == 1) return base;
if (n % 2 == 0) {
let p = multiply(n / 2);
return p * p;
}
return base * multiply(n - 1);
}

return multiply(count);
}
Insert cell
powS(2,10)
Insert cell
powS(2,20)
Insert cell
powS(2,30)
Insert cell
powS(2,40)
Insert cell
powS(2,50)
Insert cell
powS(2,60)
Insert cell
powS(2,70)
Insert cell
Insert cell
function powN(base, count) {
const bb = BigInt(base);

function multiply(n) {
if (n == 1) return bb;
if (n % 2 == 0) {
let p = multiply(n / 2);
return p * p;
}
return bb * multiply(n - 1);
}

return multiply(count);
}
Insert cell
powN(2,60)
Insert cell
powN(2,70)
Insert cell
powN(2,100)
Insert cell
powN(2,8000)
Insert cell
Insert cell
Insert cell
function factP(n) {
let m = [1n, 1n]
for (let i = 2; i <= n; i++) {
const res = BigInt(i) * m[i - 1];
m.push(res);
}
return m
}
Insert cell
factP(20)
Insert cell
Insert cell
function powP(base, n) {
const bb = BigInt(base)
let m = [1n, bb];
for (let i = 2; i <= n; i++) {
const res = bb * m[i - 1];
m.push(res);
}
return m;
}
Insert cell
powP(2,69)
Insert cell
powP(3,44)
Insert cell
powP(4,34)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more