This commit is contained in:
azure-pipelines[bot] 2023-03-28 07:39:06 +00:00
parent 67e01c76b4
commit 02bed41d9a
554 changed files with 652 additions and 377 deletions

2
3648.js Normal file
View file

@ -0,0 +1,2 @@
"use strict";(self.webpackChunkmy_application=self.webpackChunkmy_application||[]).push([[3648],{3648:(n,e,t)=>{t.r(e),t.d(e,{default:()=>i});const i="import 'dart:math' show Random;\n\nvoid main() async {\n print('Compute π using the Monte Carlo method.');\n await for (final estimate in computePi().take(100)) {\n print('π ≅ $estimate');\n }\n}\n\n/// Generates a stream of increasingly accurate estimates of π.\nStream<double> computePi({int batch = 100000}) async* {\n var total = 0; // Inferred to be of type int\n var count = 0;\n while (true) {\n final points = generateRandom().take(batch);\n final inside = points.where((p) => p.isInsideUnitCircle);\n\n total += batch;\n count += inside.length;\n final ratio = count / total;\n\n // Area of a circle is A = π⋅r², therefore π = A/r².\n // So, when given random points with x ∈ <0,1>,\n // y ∈ <0,1>, the ratio of those inside a unit circle\n // should approach π / 4. Therefore, the value of π\n // should be:\n yield ratio * 4;\n }\n}\n\nIterable<Point> generateRandom([int? seed]) sync* {\n final random = Random(seed);\n while (true) {\n yield Point(random.nextDouble(), random.nextDouble());\n }\n}\n\nclass Point {\n final double x;\n final double y;\n\n const Point(this.x, this.y);\n\n bool get isInsideUnitCircle => x * x + y * y <= 1;\n}\n"}}]);
//# sourceMappingURL=3648.js.map