Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active February 21, 2023 08:22
Show Gist options
  • Save genkuroki/45e47f56799aee57c3dc11cda6df869a to your computer and use it in GitHub Desktop.
Save genkuroki/45e47f56799aee57c3dc11cda6df869a to your computer and use it in GitHub Desktop.
Julia/find pi by julia.ipynb
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# numpyを使ったモンテカルロ法で円周率を計算"
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\nimport time as tm\n\ndef findpi(n):\n v = np.random.rand(2, n)\n count = ((v*v).sum(axis=0) <= 1).sum()\n print(4 * count / n)",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "t0 = tm.time()\nfindpi(10**9)\nt1 = tm.time() - t0\nprint(t1,\"sec\")",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "-1.153443272\n210.69541788101196 sec\n",
"name": "stdout"
},
{
"output_type": "stream",
"text": "C:\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:7: RuntimeWarning: overflow encountered in long_scalars\n import sys\n",
"name": "stderr"
}
]
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "def findpi_split1(n):\n split_unit = 10**4\n nn = int(n / split_unit)\n count_all = 0.0\n for i in range(0,nn):\n v = np.random.rand(2, split_unit)\n count = ((v*v).sum(axis=0) <= 1).sum()\n count_all += count\n print(4 * count_all / n)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "t0 = tm.time()\nfindpi_split1(10**9)\nt1 = tm.time() - t0\nprint(t1,\"sec\")",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "3.141603472\n22.548526287078857 sec\n",
"name": "stdout"
}
]
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/45e47f56799aee57c3dc11cda6df869a"
},
"gist": {
"id": "45e47f56799aee57c3dc11cda6df869a",
"data": {
"description": "Python3/find pi by numpy.ipynb",
"public": true
}
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment