Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Slices Introduction To Slices

In preparing for this lesson I came across this: The following list comprehension creates the Pythagorean triples

I'm not particularly math minded but with the help of programming I like exploring stuff. So I was wondering, is there a laymans explanation for why Pythagorean start with 345? and here is the formula:

pthag = [(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]
# I get that this is the part that calculates the starting point
# but why is it this?
if x**2 + y**2 == z**2]
# does this formula calculate this?

square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.

Alex, is this what you are referring to?

  • "The square of a positive number is geometrically the area of a square with that corresponding side length."

There's some part of my brain that gets that...but it's not the cognitive part. Thanks for sharing that link :D

1 Answer

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

If your question is just about why there is a tuple (3,4,5) ,read this. 3^2+4^2=5^2(it means the right triangle has three lines. The lenght of three lines are 3 ,4 and 5. Of couse the longest side 5 is the hypotenuse. You can learn more by google searching 3 4 5 triangle ,and you will know more.)

does it calculate this?x*2 + y2 == z*2]? Yes! since 3^2+4^2=5^2

if your question is about why there is no x=1 in the tuple, read this.

Let me just explain everything in comment One more question, What grade r u in? Or are you in college?

pthag = [(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]


#if you input pthag,you will get this
[(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (10, 24, 26), (12, 16, 20), (15, 20, 25), (20, 21, 29)]

#  If the x =1 ,then another shorter length must be √3,and the longest length is  2
#Here is the proof.https://www.khanacademy.org/math/geometry/hs-geo-trig/hs-geo-special-right-triangles/v/30-60-90-triangle-side-ratios-proof

#However, why there is no x=1? That is because if u r using range ,range can only use an argument that is a integer ,not a float.
#if you type range(1.0 ,2.0) ,you will get a exception,which it says it can only take a integer ,not float.

Thanks Jonathan, that helps a bit more and lots to look into. As far as what grade I'm in...I'm done with college, I'm just more an arts/philosophy kind of guy. I don't even know if the college I went to even had a math department. But I still think numbers can be fun.

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

@john larson,You are welcome. I am taking cal3 class ,and i guess i will have math AA degree very soon. However, i think math is useless ,and i don't want to be a highschool teacher. What do u think? Is that math useless? Is that math useless in finding a decent job?

Oh yea, I looked up 345 triangle. That helped also. Thanks. The ratio starts at 345. But then as the first number gets larger the distance between the other numbers increases to keep the ratio with the first number. It's not static. It's like blowing up a balloon. Everything gets bigger proportionally. There's no 456 cause that's a different relationship between the numbers. And this:

  • x*2 + y2 == z*2 calculates that relationship
Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

john larson,one more thing that i want to remind you that why the there is no x=1. if the x =1 ,the y and z must be √3 and 2. Since he is using float ,it won't take or output a number that is float,such as 1.5 or 1.3333. That is why the x must be 4 first inorder to have a whole y and z number.

Here is the proof x=1/2 y = (√3)/2 ,z=1 or x=1 y=(√3) z=2.https://www.khanacademy.org/math/geometry/hs-geo-trig/hs-geo-special-right-triangles/v/30-60-90-triangle-side-ratios-proof

pthag = [(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]

[(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (10, 24, 26), (12, 16, 20), (15, 20, 25), (20, 21, 29)]

Jonathan, do you need math to get a descent job? Probably not. But you obviously are good at it. It is a very interesting way of understanding the world around us. And, it sounds like you would be miserable as a high school teacher. But there's another thing. In some circles having higher math is like a club card. It will get you in with certain groups of people. That may or may not matter to you. If you are learning program as a career THAT will get you a descent job. And the pay rivals that of a doctor.

Jonathan, I just saw your last comment. That expanded my understanding again. Thanks :D