xxxxxxxxxx
const target = 10000;
let current,result;
function setup() {
noCanvas();
current = createDiv();
current.style("margin", "1em");
result = createDiv();
result.style("font-size", "16px");
result.style("color", "black");
result.style("background-color", "#e0e0e0");
result.style("padding", "1em");
result.style("margin", "1em");
result.style("border-radius", "20px");
const pi = new DigitsOfPi();
setInterval(pi.update.bind(pi), 1);
setInterval(pi.show.bind(pi), 25);
}
class DigitsOfPi {
constructor() {
this.count = 0;
this.pi = "";
this.k = 2n;
this.a1 = 4n;
this.b1 = 1n;
this.a2 = 12n;
this.b2 = 4n;
}
show(){
current.html(`${this.count - 1}/${target}`);
result.html(this.pi);
}
update() {
const p = this.k * this.k;
const q = 2n * this.k + 1n;
this.k += 1n;
[this.a1, this.b1, this.a2, this.b2] = [
this.a2,
this.b2,
p * this.a1 + q * this.a2,
p * this.b1 + q * this.b2,
];
let d1 = this.a1 / this.b1;
let d2 = this.a2 / this.b2;
while (d1 == d2) {
this.throwToPI(d1);
this.a1 = (this.a1 % this.b1) * 10n;
this.a2 = (this.a2 % this.b2) * 10n;
d1 = this.a1 / this.b1;
d2 = this.a2 / this.b2;
}
}
throwToPI(d) {
if (this.count > target) return;
if (this.count % 10 == 0) {
d += this.count ? " " : ".<br>";
}
this.pi += d;
this.count++;
}
}