Project Euler Problem 9 (C++和Python代码实现和解析)

本文探讨了在a+b+c=1000条件下寻找唯一满足a²+b²=c²的毕达哥拉斯三元组问题,通过数学分析和编程实现,最终求得三元组的乘积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 9 : Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

1. 第9个问题: 特殊的毕达哥拉斯三元组

一个毕达哥拉斯三元组是一个包含三个自然数的集合(a, b, c),且a < b < c和 a2 + b2 = c2。例如:32 + 42 = 9 + 16 = 25 = 52

唯一存在一个毕达哥拉斯三元组,且a+b+c=1000。求abc的乘积。

2. 求解分析

a+b+c = 1000, 且a < b < c和a2 + b2 = c2。我们可以得到 a < 1000/3,b < (1000-a)/2,就找到了两个循环的判断条件,然后根据c = 1000-a-b,就可以判断一下a2 + b2 是否等于 c2,如果找到满足条件的唯一的毕达哥拉斯三元组,返回abc的乘积。

3. C++ 代码实现

在这里插入图片描述

函数checkPythagoreanTriplet()用来判断三元组a, b, c是否满足a2 + b2 = c2

函数getProductOfPythagoreanTriplet()用来找到符合条件的a, b, c三元组。

C++ 代码

#include <iostream>
#include <cassert>

using namespace std;

// #define UNIT_TEST

class PE0009
{
public:
    bool checkPythagoreanTriplet(int a, int b, int c);
    int getProductOfPythagoreanTriplet(int sumOfabc);
};

bool PE0009::checkPythagoreanTriplet(int a, int b, int c)
{    
    if (a*a + b*b == c*c)
    {
        return true;
    }
    return false;
}

int PE0009::getProductOfPythagoreanTriplet(int sum)
{
    int c; // c = sum - a - b;    a < b < c
    int product = 0;
    
    for (int a=1; a<sum/3; a++)  // sum = a+b+c > 3a => a < sum/3
    {
        for (int b=a+1; b<(sum-a)/2; b++) // c = sum-a-b > b => b < (sum-a)/2
        {
            c = sum-a-b;
            if (true == checkPythagoreanTriplet(a, b, c))
            {
                product = a*b*c;
#ifdef UNIT_TEST
                cout << "The product abc(" << a << "," << b << "," << c << ")";
                cout << " is " << a*b*c << endl;
#endif
                return product;
            }
        }
    }
    return product;
}

int main()
{
    PE0009 pe0009;

    assert(true == pe0009.checkPythagoreanTriplet(3, 4, 5)); 
    
    cout << "There exists exactly one Pythagorean triplet for which " << endl;
    cout << "a + b + c = 1000, and the product abc is " ;
    cout << pe0009.getProductOfPythagoreanTriplet(1000) << "." << endl;
    
    return 0;     
}

4. Python 代码实现

Python实现方法跟C++类似的,也使用函数checkPythagoreanTriplet()来判断三元组a, b, c是否满足a2 + b2 = c2。函数getProductOfPythagoreanTriplet()用来找到满足条件的唯一三元组abc,并返回abc的乘积。

Python 代码

def checkPythagoreanTriplet(a, b, c):
    if c*c == a*a + b*b:
        return True
    else:
        return False

def getProductOfPythagoreanTriplet(p):
    """ 
    a < b < c and a+b+c = p
    p = a+b+c > 3a => a < p/3
    c = p-a-b > b  => b < (p-a)/2
    """
    product = 1
    for a in range (1, int(p/3)):
        for b in range(a+1, int((p-a)/2)):
            c = p-a-b
            if True == checkPythagoreanTriplet(a, b, c):
                product = a*b*c
                break
    return product
        
def main():
    assert True == checkPythagoreanTriplet(3, 4, 5)
    print("There exists exactly one Pythagorean triplet for which")
    print("a + b + c = 1000, and the product abc is %d."\
          % getProductOfPythagoreanTriplet(1000))

if  __name__ == '__main__':
    main()
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值