{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# À propos de π\n", "\n", "Dans ce mini-notebook, nous rappelons deux représentations classiques de π :\n", "\n", "$$ \\pi = \\frac{C}{D} $$ \n", "$$ \\displaystyle \\pi = 4 \\sum_{k=0}^{\\infty} \\frac{(-1)^k}{2k+1} . $$\n", "\n", "La célèbre expérience des [aiguilles de Buffon](https://fr.wikipedia.org/wiki/Aiguille_de_Buffon) permet aussi d’estimer π de manière probabiliste." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math\n", "math.pi" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random, math\n", "\n", "random.seed(42) # pour reproduire la même estimation\n", "N = 100_000 # nombre de lancers\n", "l, d = 1.0, 1.0 # longueur de l’aiguille et espacement des lignes\n", "\n", "cross = 0\n", "for _ in range(N):\n", " x = random.uniform(0, d/2) # distance centre-ligne la plus proche\n", " theta = random.uniform(0, math.pi/2) # angle avec la verticale\n", " if x <= (l/2) * math.sin(theta):\n", " cross += 1\n", "\n", "pi_est = (2 * l * N) / (cross * d)\n", "pi_est" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "def buffon_once(n):\n", " \"\"\"Une estimation de π par Buffon sur n lancers\"\"\"\n", " crosses = sum(\n", " random.uniform(0, d/2) <= (l/2) * math.sin(random.uniform(0, math.pi/2))\n", " for _ in range(n)\n", " )\n", " return (2 * l * n) / (crosses * d)\n", "\n", "random.seed(0)\n", "ests = [buffon_once(2_000) for _ in range(1_000)]\n", "\n", "plt.hist(ests, bins=30)\n", "plt.axvline(math.pi, linestyle='--')\n", "plt.title(\"Distribution des estimations de π (méthode de Buffon)\")\n", "plt.xlabel(\"π estimé\")\n", "plt.ylabel(\"Fréquence\")\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.10" } }, "nbformat": 4, "nbformat_minor": 5 }