You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

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'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,400

Why not just use Euclid's formula?

Here lies the reader who will never open this book. He is forever dead.

Offline

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'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 84,664

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}`

I have the result, but I do not yet know how to get it.

All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

Offline

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'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 84,664

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
```

I have the result, but I do not yet know how to get it.

All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

Offline

Who is cao?

I do not get your code.

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 84,664

That is the guys name.

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

I have the result, but I do not yet know how to get it.

All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

Offline

What does compiling Marhematica code mean?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 84,664

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.

I have the result, but I do not yet know how to get it.

All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

Offline

Why would you do that?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 84,664

Because code might run dozens of times faster!

I have the result, but I do not yet know how to get it.

All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

Offline

Pages: **1**