diff --git a/docs/examples.md b/docs/examples.md index d062c40a..58dfdf87 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -28,6 +28,12 @@ Here are some fun programs for your @boardname@! "description": "Simulation in LEDs. Use button A for next stage. Button B for reset.", "url":"/examples/gameofLife", "cardType": "example" +}, +{ + "name": "Pi Monte Carlo", + "description": "Approximate the number Pi", + "url": "/examples/pi", + "cardType": "example" } ] ``` diff --git a/docs/examples/pi-montecarlo.md b/docs/examples/pi-montecarlo.md new file mode 100644 index 00000000..72addf30 --- /dev/null +++ b/docs/examples/pi-montecarlo.md @@ -0,0 +1,53 @@ +# Pi Monte Carlo + +Approximate the value of **pi** using your @boardname@! + +```blocks +let pir = 0 +let pid = 0 +let y = 0 +let pin = 0 +let x = 0 +let r2 = 0 +let r = 0 +let inside = 0 +let n = 0 +// A simple Monte-Carlo simulation to approximate Pi. +// +// number of points +// +n = 1000000 +// radius of the circle +r = 4000 +// radius square +r2 = r * r +basic.forever(() => { + inside = 0 + for (let i = 0; i < n; i++) { + // generate a point within the square + x = Math.random(r + 1) + y = Math.random(r + 1) + // test if the point is within the circle + // sqrt(x**2 + y**2) < r ==> x**2 + y**2 < r**2 + if (x * x + y * y < r2) { + inside += 1 + } + } + // surface of a square: 4 * r * r surface of a circle: + // r * r * pi => inside / n ~= (r*r*pi) / (4*r*r) ~= + // pi / 4 pi = inside / n * 4 + pin = inside * 4 + // only integer arithmetic here... + pid = pin / n + pir = pin % n + // show results + basic.showLeds(` + # # # # # + . # . # . + . # . # . + . # . # . + . # . . # + `) + basic.showString(" " + pid + "." + pir) +}) +``` \ No newline at end of file diff --git a/docs/packages.md b/docs/packages.md index 699adb07..7c7379f8 100644 --- a/docs/packages.md +++ b/docs/packages.md @@ -11,6 +11,10 @@ "name": "NeoPixel", "url":"/pkg/microsoft/pxt-neopixel", "cardType": "package" +},{ + "name": "micro:turtle", + "url":"/pkg/microsoft/pxt-microturtle", + "cardType": "package" },{ "name": "MAX6675", "url":"/pkg/microsoft/pxt-max6675",