Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5213
#Lab 1: Plotting legs (a,b) of Primitive Pythagorean Triples reset
<function reset at 0x7f3837eaf050>
#1. The function makes a list of pairs [m,n] of rationals m/n from N diagonals of the table of rationals #//Input:? #//Output:? def mn_pairs(N): L=[]; for n in range(2,N+2): temp=[[m,n] for m in range(1,n)] L.extend(temp) return L
#Example L=mn_pairs(10);L
[[1, 2], [1, 3], [2, 3], [1, 4], [2, 4], [3, 4], [1, 5], [2, 5], [3, 5], [4, 5], [1, 6], [2, 6], [3, 6], [4, 6], [5, 6], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [1, 8], [2, 8], [3, 8], [4, 8], [5, 8], [6, 8], [7, 8], [1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [6, 9], [7, 9], [8, 9], [1, 10], [2, 10], [3, 10], [4, 10], [5, 10], [6, 10], [7, 10], [8, 10], [9, 10], [1, 11], [2, 11], [3, 11], [4, 11], [5, 11], [6, 11], [7, 11], [8, 11], [9, 11], [10, 11]]
# 2.The function makes a list of legs [a,b] of PTs and excludes PTs that are not primitive (i.e., pairs with a and b are not mutuallly prime) #//Input: #//Output:number of PTs in L,number of PPT in L,list of PPTs def ppt_legs(L): p_triples=[[c[1]^2-c[0]^2,2*c[0]*c[1]] for c in L ]#list of PTs pp_triples=[t for t in p_triples if gcd(t[0],t[1]) == 1]#list of PPTs return(len(p_triples),len(pp_triples),pp_triples)
#Example continued
n_pt,n_ppt,S=ppt_legs(L);n_pt #Convenient way to get multiple output
55
n_ppt
27
#Example of plotting points in SageMath point(S,size=30,color='red',figsize=[4,3])
#3. Main code #//Input: Number of diagonals in the table of rationals #//Output:? def plot_ppt(N): L=mn_pairs(N) n_pt,n_ppt,S=ppt_legs(L) G=point(S,size=30,color='red',figsize=[5,4]) G.show() return("Number of PTs:", n_pt, "Number of PTs:",n_ppt)
plot_ppt(20)
('Number of PTs:', 210, 'Number of PTs:', 92)
#Example of alternative plotting points in S using Python libraries # Get data for plotting points x=[s[0] for s in S];y=[s[1] for s in S]
import matplotlib.pyplot as plt from matplotlib.pyplot import figure figure(num=None, figsize=(5, 3)) figure=plt.plot(x, y,'ro',markersize=4) plt.show(figure)
#Advanced nice snippet of code for plotting (just FYI) import numpy as np import matplotlib.pyplot as plt #evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show()
[<matplotlib.lines.Line2D object at 0x7fd118429510>, <matplotlib.lines.Line2D object at 0x7fd118429650>, <matplotlib.lines.Line2D object at 0x7fd118429dd0>]
#For instructor_________________________________ #Data for detecting missed points xblue=[195,187,171,115,75,27];yblue=[28,84,140,252,308,364] #figure=plt.plot(xblue,yblue,'bo',markersize=4)# Include into the plotting command code above
#4 Find the missing point in the figure created in Part 3 (see directions in Chapter 2) gcd(147,196)
49
#5 Write a brief summary (3-4 sentences). See directions in Chapter 2. ︠db102b61-bfa2-48cd-8ebf-7beeb0fd6662︠