Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

## #1 2014-04-24 02:00:15

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 23,536
Website

### Primitive Pythagorean triples

Is the following a good way to generate primitive pythagorean triples?

``````pythag[n_] := Block[{soln = Solve[{a^2 + b^2 == c^2,  c <= n, 0 < a < b < c}, {a, b, c}, Integers]},
{Length[soln], Count[GCD[a, b] == GCD[b, c] == GCD[c, a] == 1 /. soln, True]}
]``````

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

## #2 2014-04-24 02:03:04

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,015

### Re: Primitive Pythagorean triples

Why not just use Euclid's formula?

Here lies the reader who will never open this book. He is forever dead.
Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

## #3 2014-04-24 02:06:32

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 23,536
Website

### Re: Primitive Pythagorean triples

I would love to but I do not know how to do that on M.

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

## #4 2014-04-24 03:45:49

bobbym
From: Bumpkinland
Registered: 2009-04-12
Posts: 103,723

### Re: Primitive Pythagorean triples

Here are some I used:

``somepytriples[m_]:={m,((m^2-1)/2),((m^2+1)/2)}``
``morepytriples[u_,v_,r_]:={(u^2-v^2)r,2*u*v*r,(u^2+v^2)r}``
``pytriples[m_,n_]:={n^2-m^2,2m*n,n^2+m^2}``

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
A number by itself is useful, but it is far more useful to know how accurate or certain that number is.

Online

## #5 2014-04-24 03:55:44

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 23,536
Website

### Re: Primitive Pythagorean triples

How do I iterate m and n through ranges?

I have found yet another truly marvelous algorithm.

http://stackoverflow.com/questions/18294496/proof-pythagorean-triple-algorithm-is-faster-by-euclids-formula

The last comment therein

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

## #6 2014-04-24 04:02:07

bobbym
From: Bumpkinland
Registered: 2009-04-12
Posts: 103,723

### Re: Primitive Pythagorean triples

The search will be on for one that can do what you want as soon as you define what you want.

Here is another one courtesy of Chyanog cao:

``Pick[#, (#^2).{1, 1, -1}, 0] &@Subsets[Range[200], {3}]``

For some really fast code you must go to the gurus.

http://mathematica.stackexchange.com/qu … ean-triple

Mr Wizards code, woof!

``````genPTunder[lim_Integer?Positive] :=
Module[{prim},
prim = Join @@
Table[If[
CoprimeQ[m, n], {2 m n, m^2 - n^2, m^2 + n^2}, ## &[]], {m, 2,
Floor@Sqrt@lim}, {n, 1 + m~Mod~2, m, 2}];
Union @@ (Range[lim~Quotient~Max@#]~KroneckerProduct~{Sort@#} & /@
prim)]

genPTunder[200] // Length``````

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
A number by itself is useful, but it is far more useful to know how accurate or certain that number is.

Online

## #7 2014-04-24 04:12:13

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 23,536
Website

### Re: Primitive Pythagorean triples

Who is cao?

I do not get your code.

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

## #8 2014-04-24 04:17:22

bobbym
From: Bumpkinland
Registered: 2009-04-12
Posts: 103,723

### Re: Primitive Pythagorean triples

That is the guys name.

Neither of those was written by me. RIPOSTP, produced those.

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
A number by itself is useful, but it is far more useful to know how accurate or certain that number is.

Online

## #9 2014-04-24 04:30:18

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 23,536
Website

### Re: Primitive Pythagorean triples

What does compiling Marhematica code mean?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

## #10 2014-04-24 04:33:35

bobbym
From: Bumpkinland
Registered: 2009-04-12
Posts: 103,723

### Re: Primitive Pythagorean triples

It means producing superfast  pseudocode. Not exactly machine instructions but code that M can execute much faster. M can also be converted into C++ code.

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
A number by itself is useful, but it is far more useful to know how accurate or certain that number is.

Online

## #11 2014-04-24 04:41:31

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 23,536
Website

### Re: Primitive Pythagorean triples

Why would you do that?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

## #12 2014-04-24 04:42:24

bobbym
From: Bumpkinland
Registered: 2009-04-12
Posts: 103,723

### Re: Primitive Pythagorean triples

Because code might run dozens of times faster!

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
A number by itself is useful, but it is far more useful to know how accurate or certain that number is.

Online

## #13 2014-11-10 18:53:18

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 23,536
Website

### Re: Primitive Pythagorean triples

How do I figure out which is the triple, not nexessarily primitive, (p,q,r) such that p+q+r is minimised and p>=20

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

## #14 2014-11-10 22:24:10

bobbym
From: Bumpkinland
Registered: 2009-04-12
Posts: 103,723

### Re: Primitive Pythagorean triples

Did you try to program it?

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
A number by itself is useful, but it is far more useful to know how accurate or certain that number is.

Online