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