Skip to content

Commit

Permalink
Uploaded 1st IPythonNotebook of simple algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
gspeed0689 authored Mar 18, 2021
0 parents commit c4f2efb
Showing 1 changed file with 173 additions and 0 deletions.
173 changes: 173 additions & 0 deletions Different Ways of Calculating Pi.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"math.pi"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implementing the methods for calculating pi from the WikiHow article on the subject \n",
"https://www.wikihow.com/Calculate-Pi#:~:text=The%20circumference%20of%20a%20circle%20is%20found%20with%20the%20formula,result%20should%20be%20roughly%203.14.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Gregory-Leibniz Series**\n",
"\n",
"This series takes many calculations to reach even a few accurate decimal places of pi. The calculation is done by alternating adding and subtracing calculations of 4 divided by an odd integer, starting with 0 add 4 divided by 1. According to the WikiHow article, it takes 500,000 calculations to obtain 5 accurate digits of pi. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.14159264958921"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def GregoryLeibnizSeries(endInteger):\n",
" pi = 0\n",
" current_operator = \"+\"\n",
" for i in range(1, endInteger, 2):\n",
" current_calculation = 4/i\n",
" if current_operator == \"+\":\n",
" pi += current_calculation\n",
" current_operator = \"-\"\n",
" elif current_operator == \"-\":\n",
" pi -= current_calculation\n",
" current_operator = \"+\"\n",
" return(pi)\n",
"\n",
"GregoryLeibnizSeries(500000000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Nilakantha Series**\n",
"\n",
"This series converges faster than the *Gregory-Leibniz Series*, but is a mildly more complex calculation. Again, alternating addition and subtraction, starting with addition, and dividing 4 by a value. This time, the value 4 is divided by is three numbers multiplied together, starting with an even number and the next two numbers are that even number plus 1 and plus 2. So, the first two calculations are starting with 3, add 4 divided by (2 × 3 × 4), and then subtract 4 divided by (4 × 5 × 6). "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589787"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def NilakanthaSeries(endInteger):\n",
" pi = 3\n",
" current_operator = \"+\"\n",
" for i in range(2, endInteger, 2):\n",
" current_calculation = 4/(i*(i+1)*(i+2))\n",
" if current_operator == \"+\":\n",
" pi += current_calculation\n",
" current_operator = \"-\"\n",
" elif current_operator == \"-\":\n",
" pi -= current_calculation\n",
" current_operator = \"+\"\n",
" return(pi)\n",
"\n",
"NilakanthaSeries(500000000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Pi using sine** \n",
"\n",
"This one is easier, pick a big number, multiple that big number by the sine of 180 / big number, sine needs to be in degrees. In the python code below you can see the sin calculation is converting degrees to radians with `math.radians` because trig functions in the `math` library calculate with radians. "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"\n",
"def LimitsCalculation(BigNumber):\n",
" current_calculation = BigNumber * math.sin(math.radians(180/BigNumber))\n",
" return(current_calculation)\n",
"\n",
"LimitsCalculation(900000000000)"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit c4f2efb

Please sign in to comment.