import matplotlib.pyplot as plt
xlist = [1, 2, 3]
ylist = [0, 1, 7]
plt.plot(xlist, ylist, 'g--', ms=20, alpha=0.5)
plt.show()
Definition A Pythagorean triple is a triple of positive integers $(a, b, c)$ such that $$a^2 + b^2 = c^2 $$
Example: (3, 4, 5) is a Pythagorean triple.
Exercise Write a function issquare(n)
that returns True if n
is a square of some integer and False otherwise.
def issquare(n):
return n**0.5 == round(n**0.5)
issquare(4)
issquare(100)
issquare(101)
squares = [n for n in range(101) if issquare(n)]
print(squares)
def ptriples(n):
pt = []
for a in range(1, n+1):
for b in range(1, n+1):
if issquare(a**2 + b**2):
pt.append([a, b, int((a**2 + b**2)**0.5)])
return pt
print(ptriples(20))
pt = ptriples(500)
alist = [triple[0] for triple in pt]
blist = [triple[1] for triple in pt]
plt.figure(figsize=(10,10))
plt.plot(alist, blist, 'r.')
plt.show()
Note: The lines show up because multiples of Pythagorean triples are Pythagorean triples.
t = [8, 15, 17] # Pythagorean triple
a_mult = [t[0]*n for n in range(1, 30)] # list of multiples of the first element of t
b_mult = [t[1]*n for n in range(1, 30)] # list of multiples of the second element of t
plt.figure(figsize=(10,10))
plt.plot(a_mult, b_mult, 'co', ms=12)
pt = ptriples(500)
alist = [triple[0] for triple in pt]
blist = [triple[1] for triple in pt]
plt.plot(alist, blist, 'r.')
plt.show()
Definition A Primitive Pythagorean triple is a Pythogorean triple $(a, b, c)$ such that $gcd(a, b) = 1$.
$\gcd(a, b)$ is the greatest integer that divides both $a$ and $b$.
If $\gcd(a, b)= 1$ then we say that $a$ and $b$ are relatively prime.