Finding Pythagorean triples
For this tutorial you may need to read the Wikipedia page about Pythagorean triple (for more information on Pythagorean triple visit http://en.wikipedia.org/wiki/Pythagorean_triple). Pythagorean triples are closely related to the Pythagorean Theorem, which you probably have learned about in high school geometry.
Pythagorean triples represent the three sides of a right triangle, and therefore, obey the Pythagorean Theorem. Let's find the Pythagorean triple that has a components sum of 1000. We will do this using Euclid's formula:
In this example we will see some universal functions in action.
How to do it...
The Euclid's formula defines indices m
and n
.
Create
m
andn
arrays.We will create arrays to hold these indices:
m = numpy.arange(33) n = numpy.arange(33)
Calculate a, b, and c of the Pythagorean triple.
The second step is to calculate
a
,b
, andc
of the Pythagorean triple using Euclid's formula. Use theouter
function to get the Cartesian products, difference, and...