Public
Edited
Feb 10, 2023
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Generate an image of the nth legal kings row of length 8
function row_pic(n) {
let row_pic = Plot.plot({
width: 800,
height: 100,
margin: 0,
x: { domain: [0, 8] },
y: { domain: [0, 1] },
marks: [
Plot.rect(d3.range(1, 10, 2), {
x1: (d) => d,
x2: (d) => d + 1,
y1: 0,
y2: 1,
fill: "#ddd",
stroke: "#ddd"
}),
Plot.line(
[
[0, 0],
[8, 0],
[8, 1],
[0, 1],
[0, 0]
],
{ stroke: "#ccc" }
)
]
});

let matches = rows[n].matchAll(/K/g);
let match_result = matches.next();
while (!match_result.done) {
d3.select(row_pic)
.append("g")
.attr(
"transform",
`translate(${row_pic
.scale("x")
.apply(match_result.value.index)}) scale(2.2)`
)
.append(() => svg`${king}`);
match_result = matches.next();
}

return row_pic;
}
Insert cell
// Recursive construction of the set of all legal rows of length 8
rows = {
let extend1 = (s) => s + "1";
let extend2 = (s) => s + "1K";
let rows = [
["K", "1"],
["1K", "K1", "11"]
];

for (let i = 0; i < 6; i++) {
rows.push([rows[0].map(extend2), rows[1].map(extend1)].flat());
rows.shift();
}
return rows[1].reverse();
}
Insert cell
// https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces
king = `<svg xmlns="http://www.w3.org/2000/svg" width="45" height="45">
<g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5">
<path stroke-linejoin="miter" d="M22.5 11.63V6M20 8h5"/>
<path fill="#fff" stroke-linecap="butt" stroke-linejoin="miter" d="M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5"/>
<path fill="#fff" d="M12.5 37c5.5 3.5 14.5 3.5 20 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-2.5-7.5-12-10.5-16-4-3 6 6 10.5 6 10.5v7"/>
<path d="M12.5 30c5.5-3 14.5-3 20 0m-20 3.5c5.5-3 14.5-3 20 0m-20 3.5c5.5-3 14.5-3 20 0"/>
</g>
</svg>`
Insert cell
function fibonacci(n) {
let a = 0;
if (n == 0) {
return a;
}
let b = 1;
if (n == 1) {
return b;
}
if (n > 102) {
a = BigInt(a);
b = BigInt(b);
}
for (let i = 0; i < n - 1; i++) {
let F = a + b;
a = b;
b = F;
}
return b;
}
Insert cell
import { step_slider } from "59730aabd7354b99"
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