[英]How do I make my special right triangle program work?
Write a program in python to list the special trianglesperimeters of which are less than 500 (no user input).用python写一个程序,列出周长小于500的特殊三角形(无用户输入)。
I tried writing this but it didn't seem to work.我试着写这个,但它似乎没有用。
from math import sqrt
natural_to_500_a = 1
natural_to_500_b = 1
while natural_to_500_a <= 499 and natural_to_500_b <= 499:
natural_to_500_a += 1
natural_to_500_b += 1
h_side = sqrt(natural_to_500_a**2 + natural_to_500_b**2)
print(h_side)
edit: i wrote this code it gave me the same wrong answers again编辑:我写了这段代码,它又给了我同样的错误答案
from math import sqrt
for a in range(0, 501):
c = sqrt(a**2 + a**2)
print(c)
There are better ways to compute Pythagorean triples, but this is simple:有更好的方法来计算毕达哥拉斯三元组,但这很简单:
for a in range(1,500):
for b in range(1,500-a):
for c in range(1,500-a-b):
if a**2+b**2 == c**2:
print(a,b,c)
Note that the ranges ensure that the perimeter is less than 500.请注意,范围确保周长小于 500。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.