Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

pythagorean triples

by Pete_I (Beadle)
on Apr 23, 2006 at 20:01 UTC ( #545156=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info -
Description: calculates pythagorean triples

explanation: a pythagorean triple is a set of non-zero integers that make the pythagorean theorem return true when they're plugged in.

3,4,5 is the first triple.
sqrt((3)**2 + (4)**2) = 25
sqrt(9 + 16) = 25
sqrt(25) == 5

I've got no idea what uses this could have, but I thought it was interesting.
it looked cleaner at the beginning, but map{} and for(a..b) had big memory problems.
If you find a use for it, i'd like to know. Enjoy :)
#generate pythagorean triples until n = ARGV[1]
#based on this:
#a, b, c = n^2 - m^2, 2*m*n, n^2 + m^2
my($m, $n, $end);
$end  = $ARGV[0] || 5;
for($n = 1; $n <= $end; $n++) {
 for($m = 1; $m < $n; $m++) {
  printf("%d,%d,%d\n", ($n * $n - $m * $m), (2 * $m * $n), ($n * $n + 
+$m * $m));
Replies are listed 'Best First'.
Re: pythagorean triples
by Scott7477 (Chaplain) on Apr 24, 2006 at 03:37 UTC
    Any perlmonks who happen to be members of what Wikipedia refers to as "the mysterious religious and scientific society called Pythagoreans" might like this;)
    Maybe it's useful for forecasting the future:)..The Wikipedia page Pythagoras states that "Pythagoras and his students believed that everything was related to mathematics and thought that everything could be predicted and measured in rhythmic patterns or cycles."
Re: pythagorean triples
by chas (Priest) on Apr 24, 2006 at 03:16 UTC
    But that doesn't really give "all" of them - e.g. 9,12,15 isn't of that form, is it? (Of course it is a multiple of 3,4,5 which is of that form...)
      That's correct, but any integer multiple of a primitive Pythagorean triple is also a Pythagorean triple. (The proof is left as an exercise for the reader) This sequence generates the set of primitive Pythagorean triples (assuming that $m & $n are coprime)


      The only easy day was yesterday

        Well, it's true that the primitive triples are of that form, but not conversely; 6,8,10 occurs for n=3,m=1 (which are coprime), and that triple isn't primitive. So the code generates primitive (i.e. having no common factor) triples, but some others as well. The real point is that it isn't so easy to print a list of all Pythagorean triples without duplication, and I guess that thought was what motivated my reply.

      True, but it's fairly easy to filter out the non-primitives.

      If m and n are both even, skip (a, b, and c will all be divisible by two).

      If m and n are both odd, skip (a, b, and c will all be divisible by two).

      If m and n have a divisor in common (e.g., GCD{12,3} != 1) skip (a, b, and c will have that factor in common).

      The first case can be wrapped up in third case (actually, it can be wrapped up in the second case too), but I find that it makes things clearer to separate them out.

        The third case should of course have been written as:

        If m and n have a divisor in common (e.g., GCD{m,n} != 1) skip (a, b, and c will have that divisor in common).

        Sorry about that

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://545156]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2023-01-30 15:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found