J Labs

Pythagorean Triples

(1 of 18) PYTHAGOREAN TRIPLES

This is a companion to the Beauregard and Suryanarayan treatment of Pythagorean Triples in The College Mathematics Journal:

```Beauregard, R.A., and E.R. Suryanarayan, Pythagorean```
```Triples: the Hyperbolic View```
```The College Mathematics Journal, Vol 27, No. 3, May 1996```

A vector such as 3 4 5, whose last element is the hypotenuse of the right-triangle with a pair specified by the first two, is called a pythagorean triple (PT) if the hypotenuse is an integer. We may define and use a hypotenuse function c as follows:


In [ ]:
c=: %: @: (+/) @: *:

In [ ]:
c 3 4

In [ ]:
c 5 12

In [ ]:
pt=: ],c  NB. Pythagorean triple

In [ ]:
pt 3 4

(2 of 18) PYTHAGOREAN TRIPLES (ctd)

We may assign more mnemonic names to the primitive functions used, and re-define c and pt in terms of them, as follows:


In [ ]:
on=: @:

In [ ]:
c=: sqrt on sum on sqr

In [ ]:
pt=: (right,c) "1

In [ ]:
sqrt=: %:

In [ ]:
sqr=: *:

In [ ]:
sum=: +/

In [ ]:
l=:  left=: [

In [ ]:
r=: right=: ]

In [ ]:
a=: first=: {.

In [ ]:
b=:  last=: {:

In [ ]:
v=: 3 4

In [ ]:
c v

In [ ]:
pt v

(3 of 18) THE SEMIGROUP OF TRIPLES

In the section of this title, B&S, i.e. Beauregard and Suryanarayan, define a function of two arguments that they denote by *. Applied to the pairs of two PTs it can be shown to produce the pair of a new PT. We will denote the function by p, and define it as specified in B&S:


In [ ]:
p=:(a on l*a on r) , (b on l*c on r)+(b on r*c on l)

In [ ]:
3 4 p  3 4

In [ ]:
pt 3 4 p 3 4

In [ ]:
3 4 p 5 12

In [ ]:
pt 3 4 p 5 12

In [ ]:
3 4 p 3 4 p 3 4

In [ ]:
pt 3 4 p 3 4 p 3 4

(4 of 18) MATRIX REPRESENTATION

B&S also define a mapping from a PT to a matrix, that we will treat as a function M to be applied to a pair. Thus:


In [ ]:
M=: _3&(]\)@(c,b,0:,b,c,0:,0:,0:,a)

In [ ]:
F=: _1 1&{@,

In [ ]:
m=: M 3 4

In [ ]:
m

In [ ]:
F m

(5 of 18) MATRIX REPRESENTATION (ctd)

The matrix products of such matrices produce matrix representations of PTs. Thus:


In [ ]:
X=: +/ . *  NB. Matrix product

In [ ]:
m X m

In [ ]:
F m X m

In [ ]:
pt F m X m

(6 of 18) PROGS_POSITION 1

B&E define the quasi-inverse of a pair and a number of its properties, which we will illustrate as follows:


In [ ]:
qi=: 1 _1&*

In [ ]:
A=: 4 3

In [ ]:
B=: 3 4

In [ ]:
qi A p B

In [ ]:
(qi A) p (qi B)

In [ ]:
qi qi A

In [ ]:
A

In [ ]:
A p qi A

In [ ]:
a A

In [ ]:
(*: a A), 0

In [ ]:
E=: 1 0  NB. The identity of p

In [ ]:
E p A

In [ ]:
A p E

In [ ]:
D=: 5 0

In [ ]:
D p D

In [ ]:
(*:@a D) * E

(7 of 18) PRIMITIVE PAIRS AND NORMALIZATION

An integer scalar multiple of a pair is itself a pair, and to provide a single representative of the entire class a primitive pair is defined as one whose elements are "in lowest terms", and contain no common factors. We also define a normalization function N that produces the primitive representative of any pair:


In [ ]:
D=: 3 * A

In [ ]:
D

In [ ]:
pt D

In [ ]:
+./D  NB. The GCD of the elements of D

In [ ]:
D % +./D

In [ ]:
N=: ] % +./

In [ ]:
N D

(8 of 18) PROGS_POSITIONS 2,3

Under these propositions, B&S refer to the final section that presents a method of obtaining two integers m and n from any primitive pair, a method that we will incorporate in a function mn. We will also define an inverse function ab. Thus:


In [ ]:
mn=: N@(a , b + c)

In [ ]:
ab=: N on (*/ , half on diff on flip on sqr)

In [ ]:
half=: -:

In [ ]:
diff=: -/

In [ ]:
flip=: |.

In [ ]:
v=: 5 12

In [ ]:
w=: flip v

In [ ]:
w

In [ ]:
mn v

In [ ]:
ab mn v

In [ ]:
mn w

In [ ]:
ab mn w

In [ ]:
x=: v p w

In [ ]:
x

In [ ]:
pt x

In [ ]:
mn x

In [ ]:
ab mn x

Note that the two case of Proposition 2 (for even and odd) are combined in mn by the use of normalization.

(9 of 18) PRIMITIVE PAIRS AND NORMALIZATION

An integer scalar multiple of a pair is itself a pair, and to provide a single representative of the entire class a primitive pair is defined as one whose elements are "in lowest terms", and contain no common factors. We also define a normalization function N that produces the primitive representative of any pair:


In [ ]:
D=: 3 * A

In [ ]:
D

In [ ]:
pt D

In [ ]:
+./D  NB. The GCD of the elements of D

In [ ]:
D % +./D

In [ ]:
N=: ] % +./

In [ ]:
N D

(10 of 18) LIBRARY


In [ ]:
on=: @:

In [ ]:
c=: sqrt on sum on sqr

In [ ]:
pt=: (right,c) "1

In [ ]:
sqrt=: %:

In [ ]:
sqr=: *:

In [ ]:
sum=: +/

In [ ]:
l=:  left=: [

In [ ]:
r=: right=: ]

In [ ]:
a=: first=: {.

In [ ]:
b=:  last=: {:

In [ ]:
p=: (a on l*a on r) , (b on l*c on r)+(b on r*c on l)

In [ ]:
M=: _3&(]\)@(c,b,0:,b,c,0:,0:,0:,a)

In [ ]:
F=: _1 1&{@,

In [ ]:
X=: +/ . *  NB. Matrix product

In [ ]:
qi=: 1 _1&*

In [ ]:
E=: 1 0  NB. The identity of p

In [ ]:
N=: ] % +./

In [ ]:
mn=: N@(a , b + c)

In [ ]:
ab=: N on (*/ , half on diff on flip on sqr)

In [ ]:
half=: -:

In [ ]:
diff=: -/

In [ ]:
flip=: |.

(11 of 18) GENERATING PAIRS

B&S point out that all pairs can be produced by applying p to certain basis pairs. We will explore this by making a table of basis pairs and applying p between each of the pairs, using the "outer product" p"1/ . Thus:


In [ ]:
] T=: 3 4,:4 3      NB. Table of basis elements

In [ ]:
T p"1/ T            NB. Table of p applied between each pair

In [ ]:
,/ T p"1/ T         NB. Table "ravelled" to a list of pairs

In [ ]:
N"1 ,/ T p"1/ T     NB. Normalized table

In [ ]:
~. N"1 ,/ T p"1/ T  NB. Nub (~.) suppresses duplicate items

In [ ]:
pt"1~. N"1 ,/ T p"1/ T

(12 of 18) GENERATING PAIRS (ctd)

We now embody these computations in a function, and finally sort the result for easy reading:


In [ ]:
g=: ~. on (,/ on (N"1 on (p"1/~))) NB. Generating function

In [ ]:
g T                    NB. Compare with earlier result

In [ ]:
sort=: /:~             NB. Define sort function

In [ ]:
sort g T               NB. Sort result

(13 of 18) GENERATING PAIRS (ctd)

Since each row of the result is a product (by p) of two of the basis pairs, they are not themselves included. To ensure that they are included, we add the identity element E to the table:


In [ ]:
] T1=: 1 0,T

In [ ]:
/:~ g T1

(14 of 18) GENERATING PAIRS (ctd)

To obtain complete results, the quasi-inverses of the bases must be added to the table. Thus:


In [ ]:
]T2=: T1, qi"1 T  NB. Append quasi-inverses

In [ ]:
/:~ g T2

In [ ]:
/:~ g^:2 T2

(15 of 18) GENERATING PAIRS (ctd)

Approximate results are due to machine arithmetic:


In [ ]:
/:~ g^:3 T2

(16 of 18) EXTENDED PRECISION

Larger integers may be handled to infinite precision by using the function x: as illustrated below:


In [ ]:
x: T2

In [ ]:
/:~ pt"1 g^:3 x: T2

(17 of 18) DISTINCT TRIANGLES

We may obtain a more abbreviated list of distinct triangles by taking the magnitude, replacing an entry such as 5 _12 by 5 12, and by sorting each row, replacing an entry such as 12 5 by 5 12, and then suppressing duplicate rows. We will also sort the result. Thus:


In [ ]:
tri=: pt sort ~. sort"1 | g^:3 x: T2

In [ ]:
tri

(18 of 18) PRIME FACTORS

We will conclude by showing the prime factors of the table of triangles, first removing the identity element, because an attempt to factor a zero would give a domain error:


In [ ]:
<@q: }. tri

End of Lab


In [ ]: