Changed around line 1
+ class PiVisualizer {
+ constructor() {
+ this.canvas = document.getElementById('pieChart');
+ this.ctx = this.canvas.getContext('2d');
+ this.digits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+ this.colors = [
+ '#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4',
+ '#FFEEAD', '#D4A5A5', '#9B7EDE', '#FFB6B9',
+ '#A8E6CF', '#DCEDC1'
+ ];
+ this.isRunning = false;
+ this.piGenerator = this.calculatePi();
+ this.totalDigits = 0;
+
+ this.setupCanvas();
+ this.setupControls();
+ }
+
+ setupCanvas() {
+ this.canvas.width = 400;
+ this.canvas.height = 400;
+ }
+
+ setupControls() {
+ document.getElementById('startBtn').addEventListener('click', () => this.start());
+ document.getElementById('pauseBtn').addEventListener('click', () => this.pause());
+ }
+
+ *calculatePi() {
+ let q = 1n;
+ let r = 180n;
+ let t = 60n;
+ let i = 2n;
+ while (true) {
+ let digit = ((i * 27n - 12n) * q + r * 5n) / (t * 5n);
+ yield Number(digit);
+ let u = i * 3n;
+ u = (u + 1n) * 3n * (u + 2n);
+ r = u * 10n * (q * (i * 5n - 2n) + r - t * digit);
+ q *= 10n * i * (i++ * 2n - 1n);
+ t *= u;
+ }
+ }
+
+ updateChart() {
+ const total = this.digits.reduce((a, b) => a + b, 0);
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
+
+ let startAngle = 0;
+ const centerX = this.canvas.width / 2;
+ const centerY = this.canvas.height / 2;
+ const radius = Math.min(centerX, centerY) * 0.8;
+
+ this.digits.forEach((count, index) => {
+ if (count === 0) return;
+
+ const sliceAngle = (count / total) * 2 * Math.PI;
+
+ this.ctx.beginPath();
+ this.ctx.moveTo(centerX, centerY);
+ this.ctx.arc(centerX, centerY, radius, startAngle, startAngle + sliceAngle);
+ this.ctx.fillStyle = this.colors[index];
+ this.ctx.fill();
+
+ startAngle += sliceAngle;
+ });
+
+ this.updateStats();
+ }
+
+ updateStats() {
+ const countDisplay = document.getElementById('countDisplay');
+ countDisplay.innerHTML = this.digits.map((count, index) =>
+ `
+ ${index}: ${count} (${((count / this.totalDigits) * 100).toFixed(1)}%)
+
`
+ ).join('');
+ }
+
+ async start() {
+ if (this.isRunning) return;
+ this.isRunning = true;
+
+ while (this.isRunning) {
+ const digit = this.piGenerator.next().value;
+ this.digits[digit]++;
+ this.totalDigits++;
+ this.updateChart();
+ await new Promise(resolve => setTimeout(resolve, 100));
+ }
+ }
+
+ pause() {
+ this.isRunning = false;
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new PiVisualizer();
+ });