## Tri-48

### January 30, 2018

There are two possibilities. If 48 is the hypotenuse of the triangle, the problem is to find two integers with squares that sum to a target value, which we examined in a previous exercise; in this case, there are no solutions in triangles:

(define (squares n) (let loop ((x (isqrt n)) (y 0) (zs '())) (cond ((< x y) zs) ((< (+ (* x x) (* y y)) n) (loop x (+ y 1) zs)) ((< n (+ (* x x) (* y y))) (loop (- x 1) y zs)) (else (loop (- x 1) (+ y 1) (cons (list x y) zs))))))

> (squares (* 48 48)) ((48 0))

The other possibility is that 48 is one of the other legs of the triangle, in which case we count up from 1, testing at each step if the hypotenuse is a square:

(define (tri n) (let loop ((a 1)) (let ((c (+ (* a a) (* n n)))) (when (square? c) (display (sort < (list a (isqrt c) n))) (newline))) (loop (+ a 1))))

> (tri 48) (14 48 50) (20 48 52) (36 48 60) (48 55 73) (48 64 80) (48 90 102) (48 140 148) (48 189 195) (48 286 290) (48 575 577) CTRL-C break>

This is intentionally an infinite loop. Beiler shows that any number expressible as 16k (such as 48) always has exactly ten solutions, so the list is exhaustive.

You can run the program at https://ideone.com/yX11Fm:

In Python. Look here here for solving the second part.

A Perl 1-liner…. we know the largest value of a side is (48^2/4 + 1) as {(n+1)^2-(n-1)^2 = 4n = 48^2} so we loop through all values – work out what the other side is… remove entries for which the other side is 0 or non-integer… finally display the results…

This gives:

14 48 50

20 48 52

36 48 60

48 55 73

48 64 80

48 90 102

48 140 148

48 189 195

48 286 290

48 575 577

Just realised there is a minor optimisation in mine to avoid duplicates… – can change the bottom of the loop to 34….

if you want to do for arbitrary N…

This was a fun exercise, thanks! To keep it interesting, i avoided a brute force search. Instead, I relied on Euclid’s formula. https://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple

There is still optimization to be done, but this iterates the innermost loop 71 times.

Let’s see: any k,a,b, with a and b coprime and of different parity (so a+b is odd) define a Pythagorean triple (k(a²+b²), 2kab, k(a²-b²)). If n = kj with j = a²+b², j must be an odd divisor of n, expressible as the sum of two squares; if n = 2kab, a and b are any coprime divisors of n/2 of different parity; if n = kj with j = a²-b² = (a+b)(a-b), j is product of two odd factors c,d of n, c > d, with a = (c+d)/2, b = (c-d)/2.

For n = 48, odd factors are just 1 and 3, even factors are 2,4,6,8,12,24,48.

Neither 1 or 3 are the sum of (non-zero) squares.

Even factors of 24 coprime to 1 are: 2,4,8,6,12,24 gives a,b,k = (2,1,12),(4,1,6),(8,1,3),(6,1,4),(12,1,2),(24,1,1)

Even factors of 24 coprime to 3 are: 2,4,8 gives a,b,k = (2,3,4),(4,3,3),(8,3,1)

Single pair of odd factors 1 and 3 give a,b,k = ((3+1)/2,(3-1)/2,12) = (2,1,16)

10 triples total.

Can argue the same way for any number of form 16p with p an odd prime to get 10 different legs. Might also get a hypotenuse if p is the sum of two squares (eg. if n = 80, p = 5, n = 16(1²+2²) – see https://en.wikipedia.org/wiki/Fermat%27s_theorem_on_sums_of_two_squares).

Here’s a brute force Python 2.7 solution that also works for other sized sides.

Output: