How to Efficiently Generate Pythagorean Triples in Python?

  • Thread starter Thread starter trollcast
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around generating Pythagorean triples efficiently using Python. Participants explore various methods, including brute force and formula-based approaches, while considering performance implications for different values of n.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a brute force Python solution for generating Pythagorean triples, expressing uncertainty about its efficiency.
  • Another participant questions the ordering of the triples and suggests that there are formulas available to generate them, though they are unsure if these formulas capture all possible triples.
  • Concerns are raised about the time complexity of the provided solution, with one participant noting that it runs in under 1 second for n=1000 but takes significantly longer for larger values like n=100,000.
  • Suggestions are made to optimize the brute force method by limiting the search space to c/sqrt(2) to reduce duplicate entries, although this only offers a modest speedup.
  • One participant mentions the relationship between some triples and their multiples, indicating that certain triples can be derived from others.

Areas of Agreement / Disagreement

Participants express varying opinions on the efficiency of the brute force method and the existence of formulas for generating all triples. There is no consensus on the best approach, and multiple competing views remain regarding optimization strategies.

Contextual Notes

Some limitations are noted, such as the potential for duplicate triples in the brute force method and the need for further exploration of formula-based approaches. The discussion does not resolve the effectiveness of the proposed optimizations.

trollcast
Gold Member
Messages
282
Reaction score
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
"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:
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.
 
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:
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.
 
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).
 

Similar threads

  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 12 ·
Replies
12
Views
4K
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K