Use local CSV instead of downloading each time

parent e391755a
{ {
"cells": [], "cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# À propos de pi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The number $\\pi$ is defined as the ratio of a circle's circumference to its diameter.\n",
"\n",
"\\[\n",
"\\pi \\approx 3.141592653589793\n",
"\\]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.141592653589793\n"
]
}
],
"source": [
"import math\n",
"print(math.pi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A classical method to estimate $\\pi$ is\n",
"[Buffon's needle experiment](https://en.wikipedia.org/wiki/Buffon%27s_needle_problem)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.143863179074447\n"
]
}
],
"source": [
"import random\n",
"import math\n",
"\n",
"def buffon_pi(n=100000):\n",
" hits = 0\n",
" for _ in range(n):\n",
" y = random.random() / 2\n",
" theta = random.random() * math.pi / 2\n",
" if y <= 0.5 * math.sin(theta):\n",
" hits += 1\n",
" return (2 * n) / hits\n",
"\n",
"random.seed(1)\n",
"print(buffon_pi(50000))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"for _ in range(50):\n",
" x = random.random()\n",
" y = random.random()\n",
" theta = random.random() * math.pi\n",
" dx = 0.05 * math.cos(theta)\n",
" dy = 0.05 * math.sin(theta)\n",
" plt.plot([x-dx, x+dx], [y-dy, y+dy])\n",
"\n",
"plt.hlines([0.25, 0.75], 0, 1, linestyles=\"dashed\")\n",
"plt.axis(\"equal\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 3",
...@@ -16,10 +136,9 @@ ...@@ -16,10 +136,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.3" "version": "3.6.4"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 2 "nbformat_minor": 2
} }
This diff is collapsed.
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment