Skip to content

Commit 2820171

Browse files
authored
Add files via upload
1 parent 485d8ac commit 2820171

29 files changed

+29
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","metadata":{"id":"Brtl6ZgJqgu2"},"source":["# COURSE: Master math by coding in Python\n","# SECTION: Number theory\n","# VIDEO: Number theory BUG HUNT (This is the file with bugs to fix!)\n","\n","\n","### https://www.udemy.com/course/math-with-python/?couponCode=202312\n","#### INSTRUCTOR: Mike X Cohen (http://sincxpress.com)\n","\n","This code roughly matches the code shown in the live recording: variable names, order of lines, and parameter settings may be slightly different."]},{"cell_type":"markdown","source":["<a target=\"_blank\" href=\"https://colab.research.google.com/github/mikexcohen/MathWithPython/blob/main/numberTheory/mathWithPython_numTheory_BUGHUNT.ipynb\">\n"," <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n","</a>"],"metadata":{"id":"JZhUk_36xwBy"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"SdkynBL7pcHc"},"outputs":[],"source":["import mat.pyp as plt\n","import numpy as NP"]},{"cell_type":"markdown","metadata":{"id":"XyJbtrT1pcHd"},"source":["Find integer solutions to $x^1+y^2=z^3$"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z_8eprrcpcHe"},"outputs":[],"source":["ints = np.arange(1,101)\n","\n","z = np.zeros((len(ints),len(ints)))\n","\n","# run the simulation\n","for x in ints:\n"," for y in ints:\n"," z[x-1,y-1] = (x**6 + y**3)**(1/1)\n","\n","\n","\n","# check for integers\n","zInts = z%1 == hello\n","\n","# now visualize\n","fig,ax = plt.subplots(1,2,figsize=(12,6))\n","fig.tight_layout(pad=5)\n","\n","# show the resulting z-values\n","h = ax[0].imshow(z)\n","fig.colorbar(h,ax=ax[0],fraction=.45)\n","ax[0].set_xlabel('y')\n","ax[0].set_ylabel('x')\n","ax[0].set_title('$z=\\sqrt[3]{x^3+y^3}$',fontsize=16)\n","\n","# show the boolean integer map\n","h = ax[1].imshow(zInts,vmin=0,vmax=1)\n","fig.colorbar(h,ax=ax[1],fraction=.045)\n","ax[1].set_xlabel('y')\n","ax[1].set_ylabel('x')\n","ax[1].set_title('Whether $z$ is int',fontsize=1)\n","\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tu4sR4IapcHe"},"outputs":[],"source":["# print out those triplets\n","\n","# run the simulation\n","for x in ints:\n"," for y in ints:\n"," z = (x**1 + y**2)**(1/3)\n","\n"," # check for integers\n"," if z%1 == 0:\n"," print('$%i^3 = %i + %i^2$' %(x,y,z))"]},{"cell_type":"markdown","metadata":{"id":"8fqrK8o3pcHe"},"source":["Compute and plot the following two sequences:\n","\n","$\\huge\n","s_1 = \\frac{-1^a}{\\ln(\\frac{1}{e^a})}\\\\\n","\\tiny\n","s_2 = \\frac{\\sqrt{a}}{\\left(\\frac{-\\pi}{2}\\right)^a}$\n","\n","for $a \\in \\mathbb{Z}$"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jw9A7SzApcHe"},"outputs":[],"source":["a = np.arange(?,?)\n","\n","s1 = np.zeros(len(a))\n","s2 = np.zeros(len(a))\n","\n","for a in a:\n"," s1[a-1] = -1*a / np.log(1/np.exp(a))\n"," s2[a-1] = np.sqrt(a) / (-pi/2)^a\n","\n","\n","fig,ax = plt.subplots(1,2,figsize=(10,4))\n","ax[0].plot(s1)\n","ax[0].set_title('Sequence $s_1$')\n","\n","ax[1].plot(s2)\n","ax[0].set_title('Sequence $s_2$')\n","\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"dC6NnolbpcHf"},"outputs":[],"source":[]}],"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.7.3"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":0}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","metadata":{"id":"Brtl6ZgJqgu2"},"source":["# COURSE: Master math by coding in Python\n","# SECTION: Number theory\n","# VIDEO: Euclid's Pythagorean triplets\n","\n","\n","### https://www.udemy.com/course/math-with-python/?couponCode=202312\n","#### INSTRUCTOR: Mike X Cohen (http://sincxpress.com)\n","\n","This code roughly matches the code shown in the live recording: variable names, order of lines, and parameter settings may be slightly different."]},{"cell_type":"markdown","source":["<a target=\"_blank\" href=\"https://colab.research.google.com/github/mikexcohen/MathWithPython/blob/main/numberTheory/mathWithPython_numTheory_EuclidTriplets.ipynb\">\n"," <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n","</a>"],"metadata":{"id":"JZhUk_36xwBy"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"JL_0UKJOj1YP"},"outputs":[],"source":["# import libraries\n","import numpy as np\n","import matplotlib.pyplot as plt\n","import sympy as sym"]},{"cell_type":"markdown","metadata":{"id":"qCq5Dnp_Rnvm"},"source":["# Euclid's Pythagorean triplets\n","\n","$a=m^{2}-n^{2}\\\\\n","b=2mn\\\\\n","c=m^{2}+n^{2}\\\\\n","m>n$\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qkkTllfibgom"},"outputs":[],"source":["# specify range of numbers\n","numberrange = np.arange(2,51)\n","\n","# initialize empty lists\n","a = []\n","b = []\n","c = []\n","\n","for m in numberrange:\n"," for n in numberrange:\n"," a.append( m**2 - n**2 )\n"," b.append( 2*m*n )\n"," c.append( m**2 + n**2 )"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tSYcX3G_o1Dz"},"outputs":[],"source":["fig,ax = plt.subplots(1,figsize=(10,7))\n","plt.scatter(a,b,c=c, marker='o',alpha=.5,linewidths=.5,edgecolors='k')\n","plt.axis('off')\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_lePYQjFwkPM"},"outputs":[],"source":["check = np.array(a)**2 + np.array(b)**2 - np.array(c)**2\n","\n","plt.plot(check,'o')\n","plt.ylabel('$y \\quad = \\quad a^2+b^2-c^2$')\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"p3D0pJBFzFFv"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"h62n_sWCAlZR"},"source":["# Exercise"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9x7cpwVnHtgP"},"outputs":[],"source":["# the code written above does not strictly conform to the equations. Find what's wrong and fix it!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EtUzR2cWHtgP"},"outputs":[],"source":["# initialize empty lists\n","a = []\n","b = []\n","c = []\n","\n","for m in numberrange:\n"," for n in range(numberrange[0],m):\n"," a.append( m**2 - n**2 )\n"," b.append( 2*m*n )\n"," c.append( m**2 + n**2 )\n","\n","fig,ax = plt.subplots(1,figsize=(7,7))\n","plt.scatter(a,b,c=c, marker='o',alpha=.5,linewidths=.5,edgecolors='k')\n","plt.axis('off')\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PYS-dL_s3zFT"},"outputs":[],"source":[]}],"metadata":{"colab":{"provenance":[]},"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.7.3"}},"nbformat":4,"nbformat_minor":0}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","metadata":{"id":"Brtl6ZgJqgu2"},"source":["# COURSE: Master math by coding in Python\n","# SECTION: Number theory\n","# VIDEO: Heron's method of square roots\n","\n","\n","### https://www.udemy.com/course/math-with-python/?couponCode=202312\n","#### INSTRUCTOR: Mike X Cohen (http://sincxpress.com)\n","\n","This code roughly matches the code shown in the live recording: variable names, order of lines, and parameter settings may be slightly different."]},{"cell_type":"markdown","source":["<a target=\"_blank\" href=\"https://colab.research.google.com/github/mikexcohen/MathWithPython/blob/main/numberTheory/mathWithPython_numTheory_HeronRoots.ipynb\">\n"," <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n","</a>"],"metadata":{"id":"JZhUk_36xwBy"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"JL_0UKJOj1YP"},"outputs":[],"source":["# import libraries\n","import numpy as np\n","import matplotlib.pyplot as plt\n","import sympy as sym"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"k1RUmn8tOKPi"},"outputs":[],"source":["# Let's test the algorithm in a simple example\n","num = 100\n","\n","# initialze the first guess\n","x = num/3\n","\n","# now run through the algorithm\n","for n in range(5):\n"," x = ( x + num/x )/2\n"," print(x)\n","\n","x"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CJXd06MIRn0d"},"outputs":[],"source":["# define the numbers to compute\n","nums2sqrt = np.linspace(2,101,50)\n","\n","# range of algorithm iterations\n","niterations = np.arange(3,9)\n","\n","\n","# initialize matrix of estimates\n","err = np.zeros((len(niterations),len(nums2sqrt)))\n","\n","\n","# loop over the numbers to compute\n","for ni,num in enumerate(nums2sqrt):\n","\n"," # loop over number of iterations\n"," for ii,iters in enumerate(niterations):\n","\n"," # initial guess\n"," x = num/3\n","\n"," # implement algorithm\n"," for n in range(iters):\n"," x = ( x + num/x )/2\n","\n"," # store error magnitude\n"," err[ii,ni] = abs(x-np.sqrt(num))\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uOQCIiLygR7C"},"outputs":[],"source":["plt.imshow(-np.log(err),aspect=10,extent=[nums2sqrt[0],nums2sqrt[-1],niterations[-1],niterations[0]])\n","plt.xlabel('Number')\n","plt.ylabel('Iterations')\n","plt.title('-log(error)')\n","plt.show()"]},{"cell_type":"code","source":[],"metadata":{"id":"1ry75mB0pFJp"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"kOLHbqhrKxb-"},"source":["# Exercise"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qig6omyHHtgW"},"outputs":[],"source":["# Exercise: Heron's mosquito space ship #13\n","# select target number to compute square root of\n","targnum = 13\n","\n","# range of starting values\n","starting = np.linspace(-targnum,targnum,40)\n","\n","# number of iterations (fixed)\n","niters = 5\n","\n","# initialize output matrix\n","sqrtAlgResults = np.zeros((len(starting),niters+1))\n","\n","\n","# loop over starting numbers\n","for idx,startnum in enumerate(starting):\n","\n"," # initialize starting number as a list!\n"," x = [startnum]\n","\n"," # now run through the algorithm\n"," for n in range(niters):\n"," betterguess = ( x[n] + targnum/x[n] )/2\n"," x.append( betterguess )\n","\n"," sqrtAlgResults[idx,:] = x\n","\n","\n","# finally, plot the results\n","fig,ax = plt.subplots(1,figsize=(8,6))\n","plt.plot(np.arange(niters+1),sqrtAlgResults.T,linewidth=2)\n","plt.xlabel('Algorithm iteration')\n","plt.ylabel('Numerical approximation')\n","plt.title('Estimating $\\sqrt{%g}$ = %g' %(targnum,sqrtAlgResults[-1,-1]))\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"d0G3dj1jHtgX"},"outputs":[],"source":[]}],"metadata":{"colab":{"provenance":[]},"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.7.3"}},"nbformat":4,"nbformat_minor":0}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","metadata":{"id":"Brtl6ZgJqgu2"},"source":["# COURSE: Master math by coding in Python\n","# SECTION: Number theory\n","# VIDEO: Fermat's theorem\n","\n","\n","### https://www.udemy.com/course/math-with-python/?couponCode=202312\n","#### INSTRUCTOR: Mike X Cohen (http://sincxpress.com)\n","\n","This code roughly matches the code shown in the live recording: variable names, order of lines, and parameter settings may be slightly different."]},{"cell_type":"markdown","source":["<a target=\"_blank\" href=\"https://colab.research.google.com/github/mikexcohen/MathWithPython/blob/main/numberTheory/mathWithPython_numTheory_fermat.ipynb\">\n"," <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n","</a>"],"metadata":{"id":"JZhUk_36xwBy"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"JL_0UKJOj1YP"},"outputs":[],"source":["# import libraries\n","import numpy as np\n","import matplotlib.pyplot as plt\n","import sympy as sym"]},{"cell_type":"markdown","metadata":{"id":"LV4pVB6l9g1N"},"source":["There are no integer solutions to $x^3+y^3=z^3$"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Rrj_sFXQDqJN"},"outputs":[],"source":["ints = np.arange(1,101)\n","\n","z = np.zeros((len(ints),len(ints)))\n","\n","# run the simulation\n","for x in ints:\n"," for y in ints:\n"," z[x-1,y-1] = (x**3 + y**3)**(1/3)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AEFlPR729_1x"},"outputs":[],"source":["# check for integers\n","zInts = z%1 == 0\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FDE9IN_r-FhI"},"outputs":[],"source":["\n","# now visualize\n","fig,ax = plt.subplots(1,2,figsize=(12,6))\n","fig.tight_layout(pad=5)\n","\n","# show the resulting z-values\n","h = ax[0].imshow(z)\n","fig.colorbar(h,ax=ax[0],fraction=.045)\n","ax[0].set_xlabel('y')\n","ax[0].set_ylabel('x')\n","ax[0].set_title('$z=\\sqrt[3]{x^3+y^3}$',fontsize=16)\n","\n","# show the boolean integer map\n","h = ax[1].imshow(zInts,vmin=0,vmax=1)\n","fig.colorbar(h,ax=ax[1],fraction=.045)\n","ax[1].set_xlabel('y')\n","ax[1].set_ylabel('x')\n","ax[1].set_title('Whether $z$ is int',fontsize=16)\n","\n","plt.show()"]},{"cell_type":"code","source":[],"metadata":{"id":"WjMxi6rnoZuX"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"XiR9JHTk-GrY"},"source":["# Exercise: \"Fermat's stairway to heaven\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ANkgxgp4HtgQ"},"outputs":[],"source":["# Modify the code to create a map of integer Pythagorean triplets"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PYS-dL_s3zFT"},"outputs":[],"source":[]}],"metadata":{"colab":{"provenance":[]},"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.7.3"}},"nbformat":4,"nbformat_minor":0}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","metadata":{"id":"Brtl6ZgJqgu2"},"source":["# COURSE: Master math by coding in Python\n","# SECTION: Number theory\n","# VIDEO: Plotting number sequences\n","\n","\n","### https://www.udemy.com/course/math-with-python/?couponCode=202312\n","#### INSTRUCTOR: Mike X Cohen (http://sincxpress.com)\n","\n","This code roughly matches the code shown in the live recording: variable names, order of lines, and parameter settings may be slightly different."]},{"cell_type":"markdown","source":["<a target=\"_blank\" href=\"https://colab.research.google.com/github/mikexcohen/MathWithPython/blob/main/numberTheory/mathWithPython_numTheory_numberSeqs.ipynb\">\n"," <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n","</a>"],"metadata":{"id":"JZhUk_36xwBy"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"JL_0UKJOj1YP"},"outputs":[],"source":["# import libraries\n","import numpy as np\n","import matplotlib.pyplot as plt\n","import sympy as sym"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"II1kBlVORp9M"},"outputs":[],"source":["# initialize\n","s1 = np.array([])\n","s2 = np.array([])\n","s3 = np.array([])\n","s4 = np.array([])\n","xx = np.arange(1,81)\n","\n","\n","# generate the sequences\n","for n in xx:\n"," s1 = np.append(s1,n/(n+1))\n"," s2 = np.append(s2,(n+1)/n)\n"," s3 = np.append(s3,n/sum(np.arange(1,n+1)))\n"," s4 = np.append(s4,(-1)**n/np.sqrt(n))\n","\n","\n","# plotting\n","fig,ax = plt.subplots(1,figsize=(10,7))\n","plt.plot(xx,s1,label='$a/(a+1)$')\n","plt.plot(xx,s2,label='$(a+1)/a$')\n","plt.plot(xx,s3,label='$a/\\Sigma(a)$')\n","plt.plot(xx,s4,label='$-1^a/\\sqrt{a}$')\n","plt.legend(loc='upper right',fontsize=16)\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"utMhyJh-zNFz"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"JtQstBsu0xZT"},"source":["# Exercise"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kqtdrJReHtgR"},"outputs":[],"source":["# The series above are all convergent. Create a divergent series."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-Mc7_vRxHtgR"},"outputs":[],"source":["# repeat without a for-loop!\n","\n","# the a's\n","aSeq = np.arange(1,N)\n","\n","# the sequences\n","s1 = aSeq/(aSeq+1)\n","s2 = (aSeq+1)/aSeq\n","s3 = aSeq/np.cumsum(aSeq)\n","s4 = (-1)**aSeq/np.sqrt(aSeq)\n","\n","# examples of divergent sequences\n","# s3 = np.cumsum(aSeq)/aSeq\n","# s4 = np.sqrt(aSeq)/(-1)**aSeq\n","\n","# plotting\n","fig,ax = plt.subplots(1,figsize=(10,7))\n","plt.plot(xx,s1,label='$a/(a+1)$')\n","plt.plot(xx,s2,label='$(a+1)/a$')\n","plt.plot(xx,s3,label='$a/\\Sigma(a)$')\n","plt.plot(xx,s4,label='$-1^a/\\sqrt{a}$')\n","plt.legend(loc='upper right',fontsize=16)\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PYS-dL_s3zFT"},"outputs":[],"source":[]}],"metadata":{"colab":{"provenance":[]},"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.7.3"}},"nbformat":4,"nbformat_minor":0}

0 commit comments

Comments
 (0)