Generating Pythagorean Triples

  • Thread starter trollcast
  • Start date
In summary, the person is looking for a formula to generate all pythagorean triples. They timed it for n=10000 and it took about 30 seconds on their pc.
  • #1
trollcast
Gold Member
282
13

Homework Statement


Generate a list of the first n pythagorean triples.


Homework Equations


a^2=b^2+c^2


The Attempt at a Solution



This is what I've came up with in python but I'm sure its not the most efficient way I could have done it:

Code:
import math

n = int(raw_input("Input a number: "))

trips_found = 0
c = 4

while trips_found < n:
    c += 1
    csquared = c * c
    a = 3
    while a < c:
        asquared = a * a
        bsquared = csquared - asquared
        b = math.sqrt(bsquared)

        if b % 1 ==0:
            print "(" + str(a) + ", " + str(int(b)) + ", " + str(c) + ")"
            trips_found += 1

        a += 1
 
Physics news on Phys.org
  • #2
"First", ordered by what?
There are formulas to generate those triples, but I don't know if they find all options.

Runs in <1s for n=1000 and ~15s for n=10000 here, not counting output. Do you need more?Edit: You find all triples twice (with a and b switched).
 
Last edited:
  • #3
mfb said:
"First", ordered by what?
There are formulas to generate those triples, but I don't know if they find all options.

I would expect that this code does not need significant time for n<100. Do you need more?

First ordered by the largest value of the triple ie.

(3, 4, 5)
(4, 3, 5)
(6, 8, 10)
(8, 6, 10)
(5, 12, 13)
(12, 5, 13)
(9, 12, 15)
(12, 9, 15)
.
.
.

Its not too bad for time but I was just wondering if there was a more direct way of finding them since that was quite like a brute force solution.
 
  • #4
For 100 000 it takes some time now.

A first easy step would be to stop testing at c/sqrt(2). This removes all doubled entries, too (you can add them manually again if you like), but the speedup is just about this factor of 1/sqrt(2).

There are formulas which find all triples.

Edit: Oh, done! 844 seconds for n=100 000.
 
Last edited:
  • #5
mfb said:
For 100 000 it takes some time now.

A first easy step would be to stop testing at c/sqrt(2). This removes all doubled entries, too (you can add them manually again if you like), but the speedup is just about this factor of 1/sqrt(2).

There are formulas which find all triples.

I timed it for n = 10000 and it took about 30 seconds on my pc.

I'll have a look for one of those formulae to see if it would work better.
 
  • #6
I could maybe use the fact that some triples are just multiples of other triples, eg. (6,8,10) is just 2x (3,4,5).
 

1. What are Pythagorean Triples?

Pythagorean Triples are sets of three positive integers (a, b, c) that satisfy the Pythagorean Theorem, a² + b² = c². In other words, the square of the longest side (hypotenuse) of a right triangle is equal to the sum of the squares of the other two sides.

2. How do you generate Pythagorean Triples?

There are several methods to generate Pythagorean Triples, including the Euclid's formula, the Fibonacci's formula, and the brute force method. The most commonly used method is the Euclid's formula, which states that a = m² - n², b = 2mn, and c = m² + n², where m and n are any positive integers with m > n.

3. What is the significance of Pythagorean Triples?

Pythagorean Triples have been studied in mathematics for centuries and have many applications in geometry and number theory. They also have practical uses in construction and engineering, as they can be used to create right angles and determine the lengths of sides in a right triangle.

4. Are there any patterns in Pythagorean Triples?

Yes, there are several patterns in Pythagorean Triples. One of the most well-known patterns is that any Pythagorean Triple can be multiplied by a constant to create another Pythagorean Triple. For example, the Pythagorean Triple (3, 4, 5) can be multiplied by 2 to create (6, 8, 10).

5. What is the largest Pythagorean Triple?

The largest Pythagorean Triple is (784, 1573, 1735), which was discovered by mathematician Pierre de Fermat in the 17th century. However, it is believed that there are infinitely many Pythagorean Triples, and there is no known limit to the size of the triple.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
880
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
872
Back
Top