用Python实现求固定数范围内的勾股数

def find_pythagorean_triples(max_value):
    """找出所有不超过max_value的勾股数"""
    triples = []
    
    # 遍历所有可能的a、b、c值
    for a in range(1, max_value + 1):
        for b in range(a, max_value + 1):  # 从a开始避免重复
            c_squared = a**2 + b**2
            c = int(c_squared**0.5)
            
            # 检查c是否为整数且不超过最大值
            if c <= max_value and c**2 == c_squared:
                triples.append((a, b, c))
    
    return triples

# 找出25以内的勾股数
max_n = 25
pythagorean_triples = find_pythagorean_triples(max_n)

# 输出结果
print(f"{max_n}以内的勾股数有 {len(pythagorean_triples)} 组:")
for i, triple in enumerate(pythagorean_triples, 1):
    print(f"{i}. {triple} (验证: {triple[0]}^2 + {triple[1]}^2 = {triple[2]}^2 = {triple[0]**2 + triple[1]**2})")

程序执行结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值