# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/MartyWeissman/PythonForMathematics/blob/main/Math152_Jan_21_2021.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="xQRH5MK47h6f"
# # Teaching notebook, January 21, 2021

# + [markdown] id="1MjvoMX_-ASg"
# Recall the formula for $e^x$:
# $$e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \cdots.$$

# + [markdown] id="S05vUI7-8ZGp"
# Approximating e.  Solution 1

# + id="eioWYl_n7hKU"
e = 1
x = 1
for n in range(1,21):
    x = x*n
    e = e + (1/x)
    print(e)

# + [markdown] id="T21qI-_A8bsj"
# Approximating e.  Solution 2.

# + id="0vlKjmls8pKw"
'Approximating e'
coff=1
x_pow=1
Sum=1
for m in range(1,30):
  coff=((1/m)*coff)
  term=coff*(x_pow**m)
  Sum=(Sum+term)
print((Sum))

# + [markdown] id="ut6OFyPv-Wp8"
# Approximating e.  Solution 3.

# + id="YyWbOfsu-VlZ"
s=1
f=1
y=0
for x in list(range(1,18)):
  f=f*(y+1)
  y=y+1
  s=s+1/f
print(s)

# + id="VeWU3LS1DaV6"
from mpmath import *
print(mp)

# + id="DIMpwQRNDaLX"
mp.dps = 100 # Let's try 100 decimal digits precision.
print(mp)

# + id="q3DZR9X_Dcac"
sqrt(2)  # mpf(...) stands for an mp-float.

# + id="Pi8ZjaTxDd9x"
mpf(1.0)

# + [markdown] id="7NmHwFjH9fi2"
# Approximating $\pi$.  

# + id="vR2YaQcP9waz"
type(sqrt(2)) # mpmath stores numbers in its own types!

# + id="7PwLqLeS9xMB"
pi**2

# + id="5IyuooSM9h-C"
p=4*sqrt(2)
P=8
for n in range(1,166):
    P = (2*p*P)/(p + P)
    p = sqrt(p*P)
print(p/2)
print(P/2)

StarcoderdataJupyterScriptsDedupFiltered