Published
Edited
Sep 29, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function newton_sqrt(x, error_margin=0.01, a=x/2, guesses=1) {
const delta = x/a - a;
if (Math.abs(delta) > error_margin) return newton_sqrt(x, error_margin, delta/2 + a, guesses+1);
return [a, guesses];
}
Insert cell
my_answer(test__1_2(10000))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Math.pow(3/4, complexity)
Insert cell
function area_sierpinski(n) {
if (n == 0) return 1;
return area_sierpinski(n-1) / 4 * 3;
}
Insert cell
Insert cell
test__1_2 = (x) => {
const result = [
...newton_sqrt(x),
Math.sqrt(x),
];
return md`testing ${tex`x=${x}`}
- target: \`${result[2]}\`
- answer: \`${result[0]}\`
- error: \`${result[0]-result[2]}\`
- guesses: \`${result[1]}\`
`;
}
Insert cell
function sierpinksi_triangle_pts(a, x,y, w=width, n=0) {
if (n == 0) {
return a
.concat(points_ccw_from_left(x,y, w));
}
else {
const n1 = n - 1;
const w1 = w / 2;
const ref = points_ccw_from_left(x,y, w1);
return a
.concat(sierpinksi_triangle_pts(a, x,y, w1, n1)) // left
.concat(sierpinksi_triangle_pts(a, x+w1,y, w1, n1)) // right
.concat(sierpinksi_triangle_pts(a, ref.c.x,ref.c.y, w1, n1)) // top
}
}
Insert cell
points_ccw_from_left = (x,y, w) => {
// eq tri, sides = w, h forms 30/60/90 tri, so (w/2)^2 + h^2 = w^2
// h = sqrt(3) / 2 * w
const h = Math.sqrt(3) / 2 * w;
return {
a: {x: x, y: y},
b: {x: x+w, y: y},
c: {x: x+(w/2), y: y-h},
};
}
Insert cell
draw_triangle = (cv, tc) => {
const c2d = cv.getContext('2d');
const path = new Path2D();
path.moveTo(tc.a.x, tc.a.y);
path.lineTo(tc.b.x, tc.b.y);
path.lineTo(tc.c.x, tc.c.y);
path.lineTo(tc.a.x, tc.a.y);
path.closePath();
c2d.fill(path);
}
Insert cell
my_answer = (markdown) => html`<div style='background-color: #ffd'>${md`##### _my answer_ `}${markdown}</div>`
Insert cell
import {slider} from "@jashkenas/inputs"
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