MATLAB Answers

### Translated by 이 페이지의 내용은 영어에서 자동 번역되었습니다. 자동 번역 기능을 해제하려면 "<a class="turn_off_mt" href="#">여기</a>"를 클릭하십시오.

## Finding pythagoras triples when only c is known

Asked by ML

### ML (view profile)

on 7 Apr 2019
Latest activity Edited by John D'Errico

### John D'Errico (view profile)

on 8 Apr 2019
Accepted Answer by John D'Errico

### John D'Errico (view profile)

Finding a and b when c is known
Here's what I have
str='a=%4i b=%4i c=%4i';
for c=137
for a=1:200
for b=1:200
if c==sqrt(a^2+b^2)
disp(sprintf(str,a,b))
end
end
end
end
commandwindow

#### 0 Comments

Sign in to comment.

R2019a

## 4 Answers ### John D'Errico (view profile)

Answer by John D'Errico

### John D'Errico (view profile)

on 7 Apr 2019
Edited by John D'Errico

### John D'Errico (view profile)

on 8 Apr 2019
Accepted Answer

Alternatively...
Talk to Euclid. An old guy, maybe even dead by now. He gave us a formula for Pythagorean triples.
Since c=137 is prime, if a triple exists, it is in lowest form, irreducible. So we can write it as
c = u^2 + v^2
a = u^2 - v^2
b = 2*u*v
for non-negative integer u,v, relatively prime.
By inspection,
137 - (1:11).^2
ans =
136 133 128 121 112 101 88 73 56 37 16
(See? I actually used MATLAB to answer this question!) The only pair of integers that satisfy 137 = u^2 + v^2 is [11,4], thus
137 = 11^2 + 4^2 = 121 + 16
Therefore the desired Pythagorean triple is formed from
a = 11^2 - 16^2 = 105
b = 2*11*4 = 88
Of course, we can also swap a and b to get the pair of triples: [88,105,137], and [105,88,137], but they are not truly distinct. No other triples can exist for c=137.
88^2 + 105^2 == 137^2
ans =
logical
1
Addendum, for those who might be interested:
Perhaps a more interesting question is, does ANY pythagorean triple exist where one of the legs of the triangle is 137, thus a or b? Can such a triple exist? This goes beyond the original question where we had c==137. May one of the legs ever be 137?
In fact, pythagorean triples where one (or both) of a or b is prime tend to be rather rare. Rare as hen's teeth in fact. We can have a triple where a and c are both prime, but not both a and b. Triples where a is prime are not that common, but they do exist.
(That is not true where c would have been prime. Triples with c as prime are far more easy to find.) The reason is pretty simple. 137 is prime. As such, it has no integer factors other than 1 or 137. That means that any Pythagorean triple where one of the members is prime is an irreducible triple.
Again, Euclid tells us that we can write such a triple in the form
c = u^2 + v^2
a = u^2 - v^2
b = 2*u*v
where u and v are coprime, positive integers. That means b would never be 137, since 2 is not a factor of 137. But then, can we write 137 (a prime) in the form
137 = u^2 - v^2 = (u-v)*(u+v)
Because of unique factorization of the integers, that could ONLY EVER HAPPEN if we had
u = v + 1
so that u - v == 1. In that case we would have
137 = (v+1 - v) * (v+1 + v) = 1*(2*v+1)
so v would be 68, and u must be 69. The resulting Pythagorean triple is therefore:
pytriple = @(u,v) [max(u,v)^2 - min(u,v)^2, 2*u*v, u^2 + v^2];
pytriple(68,69)
ans =
137 9384 9385
137^2 + 9384^2 == 9385^2
ans =
logical
1
Therefore, we can prove that the only distinct Pythagorean triples with 137 as one of the members are [88,105,137] and [137,9384,9385].
Second addemdum: Note that in the above example, with a = 137, we found c as 9385, where c was not itself also prime. Can we find all Pythagorean triples where two members of the triple are prime? Is that a finite set?
By extension of what I said above, starting with a prime (exceeding 2) for a, find all triples that contain that number as a. We can then recognize that any odd prime is uniquely representable as a product only in the form
a = 1*(2*v + 1)
thus for prime a, we must have:
v = (a-1)/2
u = v + 1
Now just recognize which possible values of prime a also result in a prime for c, the longest side of the triangle?
So we can identify the set of all 32 Pythagorean triples with both a and c prime such that a is no larger than 1000.
a = setdiff(primes(1000),2)';
v = (a-1)/2;
u = v + 1;
k = isprime(u.^2 + v.^2);
[u(k).^2 - v(k).^2, 2*u(k).*v(k), u(k).^2 + v(k).^2]'
ans =
3 4 5
5 12 13
11 60 61
19 180 181
29 420 421
59 1740 1741
61 1860 1861
71 2520 2521
79 3120 3121
101 5100 5101
131 8580 8581
139 9660 9661
181 16380 16381
199 19800 19801
271 36720 36721
349 60900 60901
379 71820 71821
409 83640 83641
449 100800 100801
461 106260 106261
521 135720 135721
569 161880 161881
571 163020 163021
631 199080 199081
641 205440 205441
661 218460 218461
739 273060 273061
751 282000 282001
821 337020 337021
881 388080 388081
929 431520 431521
991 491040 491041
Note that a = 137 is NOT in that set, as expected, since 9385 is not a prime itself. But are there infinitely many such Pythagorean triples where two members of the triple are prime? This becomes a yet more interesting question...
We see on that page, Schinzel and Sierpinski's Hypothesis H: we then expect to see infinitely many triples with two prime entries. There they list the first few such triples as I list above, but no proof is apparently known fror that claim.
Regardless, it is often fun to just wander off, to go exploring in mathematics.
Addendum the third:
Just as I ran out of steam from the last exercise, did you notice anything strange fall out of the woodwork? Is there a good reason why the long side for all of those triangles seems to end with 1, at least if we ignore the first two triangles in the list? Whenever I see an incongruity like that, it just makes me wonder if there is something behind the idea. In fact, you can check that statement. All Pythagorean triples (beyond the first such pair) with two prime legs (I call them doubly prime) have the longest leg ( c ) ending with the digit 1.
Surely there would be some primes for c that do not end with 1. But primes that are also the long side of a doubly prime Pythagorean triple? Why does this seem an interesting coincidence?
Assume that a is an odd prime. Then as we have seen, a must be of the form a=2*v+1 for some integer v, and we must have u = v+1. The long side of the triple is
c = u^2 + v^2 = (v+1)^2 + v^2 = 2*v^2 + 2*v + 1
Now lets look at the value of the two sides of a Pythagorean triple that could possibly be prime, for all possible values of v, modulo 10. I can write any number v as one of the forms 10*w+(0:9).
syms w
v = 10*w + (0:9).';
[v, 2*v+1, expand(2*v.^2 + 2*v + 1)]
ans =
[ 10*w, 20*w + 1, 200*w^2 + 20*w + 1]
[ 10*w + 1, 20*w + 3, 200*w^2 + 60*w + 5]
[ 10*w + 2, 20*w + 5, 200*w^2 + 100*w + 13]
[ 10*w + 3, 20*w + 7, 200*w^2 + 140*w + 25]
[ 10*w + 4, 20*w + 9, 200*w^2 + 180*w + 41]
[ 10*w + 5, 20*w + 11, 200*w^2 + 220*w + 61]
[ 10*w + 6, 20*w + 13, 200*w^2 + 260*w + 85]
[ 10*w + 7, 20*w + 15, 200*w^2 + 300*w + 113]
[ 10*w + 8, 20*w + 17, 200*w^2 + 340*w + 145]
[ 10*w + 9, 20*w + 19, 200*w^2 + 380*w + 181]
The first column there is v. The second column is a, the third column is c. Now, exclude all rows of that array when either the second or third column would be divisible by 5, as then a or c could not be prime.
[ 10*w, 20*w + 1, 200*w^2 + 20*w + 1]
[ 10*w + 4, 20*w + 9, 200*w^2 + 180*w + 41]
[ 10*w + 5, 20*w + 11, 200*w^2 + 220*w + 61]
[ 10*w + 9, 20*w + 19, 200*w^2 + 380*w + 181]
If we do exactly that, we would find in all cases the only possibilities where both a and c can simultaneously be prime and not divisible by 5 will result in mod(c,10) being 1. The first two triples in our list above ([3,4,5] and [5,12,13]) don't need to follow this pattern, because 5 is the only prime that is also trivially divisible by 5.
Having now squeezed every ounce of blood from this rock that I can possibly squeeze, I am now done. Sigh.

dpb

### dpb (view profile)

on 7 Apr 2019
"Regardless, it is often fun to just wander off, to go exploring in mathematics."
Indeed, and thanks! for taking the time to post your digressions, John....
Rik

### Rik (view profile)

on 7 Apr 2019
I agree, a nice read, have a +1 from me as well. ( even if it doesn't actually answers the question for a c in general ;) )
John D'Errico

### John D'Errico (view profile)

on 7 Apr 2019
Thx to both of you. :)

Sign in to comment.

Answer by Rik

### Rik (view profile)

on 7 Apr 2019

You are quite close. The code below contains some optimization in terms of selection of values to check, and the check is modified to ignore float rounding errors.
clc
str='a=%4i b=%4i c=%4i\n';
for c=137
c2=c^2;
for a=1:(c-1)
a2=a^2;
for b=a:(c-1)
if abs(c2-(a2+b^2))<2*eps
fprintf(str,a,b,c)
end
end
end
end

dpb

### dpb (view profile)

on 7 Apr 2019
My bad; misread indeed...if just so happens that 137 = 4^2 + 11^2 and I thunk that was the target value OP was looking for. But, I see that isn't what he actually wrote, indeed.
Mea culpa! :(
John D'Errico

### John D'Errico (view profile)

on 7 Apr 2019
To me, it looked like one of those times whern I look in at 4:00 am, still a bit asleep. :)
dpb

### dpb (view profile)

on 7 Apr 2019
<vbg>

Sign in to comment.

Answer by dpb

on 7 Apr 2019
Edited by dpb

### dpb (view profile)

on 7 Apr 2019

Somewhat more rudimentary would be
UP=fix(sqrt(Csq-1)); % can't be bigger ignoring degenerate case of (sqrt(Csq^2),0)
for i=1:UP
isq=i*i;
for j=1:UP
jsq=j*j;
if isq+jsq==Csq
disp([i j])
return
end
end
end
Since are dealing with integers don't need to worry about the floating point roundoff, at least as long as a double can still hold exact value for Csq (~15 decimal digits).

dpb

### dpb (view profile)

on 7 Apr 2019
Yeah, as noted above, that 137 is 4^2 + 11^2 made me misinterpret the input as being C^2 instead of C altho OP did write otherwise...thanks.
At least I did write Csq in the Answer...how one gets it is "exercise for the Student" :)
Rik

### Rik (view profile)

on 7 Apr 2019
Just out of curiosity: would you miss any values if you let the second loop start with i instead of 1?
(And to be honest I had already written the tolerance-safe version when I decided to cut the sqrt from the test. I anticipated sqrt might have introduced some float rounding errors.)
dpb

### dpb (view profile)

on 7 Apr 2019
No, it's exhaustive to make the second loop begin at the position of the first, yes. Do, of course, have to start at j==i, not i+1 as you note to get the double cases. But this will always find the two factors in the order of smaller, larger for unequal a,b (again as long as no precision overflow).
I forget just how yours was written now, first, Rik...seemed to me at the time that rounding wouldn't enter into the test because the only cases that would matter were perfect squares...so as long as didn't overflow the precision it would be exact and if it did, then the rounding test wouldn't help, anyway. But, I won't claim I was necessarily correct! :)

Sign in to comment.

Answer by madhan ravi

### madhan ravi (view profile)

on 7 Apr 2019
Edited by madhan ravi

### madhan ravi (view profile)

on 7 Apr 2019

C2 = c^2;
[X,Y]=ndgrid(1:200);
XY=[X(:).^2,Y(:).^2];
XY(sum(XY,2)==C2,:) % first column is a and the second b which is squared, so if you apply sqrt() you get the values of a and b

#### 0 Comments

Sign in to comment.