{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# On the computation of $\\pi$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Asking the maths library\n", "My computer tells me that $\\pi$ is approximately" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import *\n", "print(pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Buffon's needle\n", "Applying the method of [Buffon's needle](https://en.wikipedia.org/wiki/Buffon%27s_needle_problem), we get the **approximation**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.random.seed(seed=42)\n", "N = 10000\n", "x = np.random.uniform(size=N, low=0, high=1)\n", "theta = np.random.uniform(size=N, low=0, high=pi/2)\n", "2/(sum(x+np.sin(theta))>1/N)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }