## Funny Math

# Having fun with Pythagoras

## How Pythagorean triples are arranged in space

Some days ago I was playing around with Plotly and JavaScript.

I remembered the high school and the funniest Math competitions. I always had some weird ideas in my mind about problems and exercises. Everything I needed was a pencil and a piece of paper.

This is what I believed. When I discovered Computer Science, I leveled up! 👾👾👾

A question that’s always been on my mind was:

How are Pythagorean triples arranged in space?

In this article you can get the answer and it’s really *awesome.* Read further!

## Let’s take a step back

What are Pythagorean triples?

They are special triads of numbers. If you take each number of a Pythagorean triple and create a triangle with such sides, you get a right triangle. 😎 *Alright?*

Why are Pythagorean triples *so beautiful*?

First of all, because there are fews. The smallest one is (3,4,5).

Each number of a triple is coprime with the other two. It means that the greatest common divisor is always one.

I believe the triples are *consonant *and in music they would sound* *like a major triad (e.g. C-E-G). 🎶 Three perfect individualities that come together in a mathematical masterpiece.

## My high school idea

If we have three numbers in a Pythagorean triple, and three dimensions in space… How are these triples arranged, if we represent them in a Cartesian three axis space?

Of course paper and pencil were no use at all. So the problem remained a beautiful mystery to me for so many years. Like an Egyptian tomb covered with sand.

## My Codepen

Out of the blue, some days ago, I decided to plot several Pythagorean triples in a Codepen (I always use it as a sandbox for my evil experiments).

My JavaScript algorithm is really simple. There are just two nested `for`

cycles. Indeed, you only need two numbers to get the third of the triple and check if it’s a Pythagorean one.

After generating three arrays of x,y and z coordinates, I draw them in Plotly.

I also put some rainbow colors to spice up the situation! 🥳

An interesting optimization: when I find a Pythagorean triple, I also add the other 5 permutation of that triple. Indeed, if (a,b,c) is a valid Pythagorean triple, then also (a,c,b), (b,a,c), (b,c,a), (c,a,b), (c,b,a) are Pythagorean triples.

In this way, I don’t need to iterate from 1 to n in both my cycles, but I can stop before in the second cycle.

I even avoid checking the couples in the (a,a) form. They surely aren’t Pythagorean triples.

`for (let i = 1; i < max; i++) {`

for (let j = 1; **j < i-1**; j++) {

// k^2 = i^2 + j^2

// check if (i,j,k) is a Pythagorean triple

}

}

Let’s test our cycle. For example, at the fourth step of our loop, we check if the following couples form Pythagorean triples:

- (1,2) → Not a triple (1+4=5) ❌
- (1,3) → Not a triple (1+9=10) ❌
- (2,3) → Not a triple (4+9=13) ❌
- (1,4) → Not a triple (1+16=17) ❌
- (2,4) → Not a triple (4+16=20) ❌
- (3,4) → Triple (9+16=25) ✔

Ok, some triples will be repeated many times, but nevermind, the points will be overlapped. Here is the final result in my Codepen.

Hope you enjoyed the article! 💙