Published
Edited
Oct 3, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable AlgorithmPerformance = 0;
Insert cell
Insert cell
{
var t0 = performance.now();
const segment = SegmentCalculation(10000, 50);
var t1 = performance.now();
mutable AlgorithmPerformance = (t1 - t0);
return 4 * (SqrtHalf * SqrtHalf + 2 * segment);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
InsideCircle = (x, y) => {
return x * x + y * y <= 1;
}
Insert cell
SegmentCalculation = (resolution, iterations) => {
const deltaX = (1 - SqrtHalf) / iterations;
const deltaY = SqrtHalf / resolution;
let sum = 0;
let prev = 1;
for(let i = 1; i < resolution - 1; i++) {
const y = i * deltaY;
const x = DaQSearch(y, iterations);
sum += deltaY * 0.5 * (x - 2 * SqrtHalf + prev);
prev = x;
}
// Last operation
sum += (prev - SqrtHalf) * deltaY;
return sum;
}
Insert cell
DaQSearch = (y, iterations) => {
// Interval
let left = SqrtHalf;
let right = 1;
for (let i = 0; i < iterations; i++) {
const mid = 0.5 * (left + right);
if (InsideCircle(mid, y)) left = mid;
else right = mid;
}
return left;
}
Insert cell
Insert cell
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