Problem 9 Special Pythagorean triplet

Problem 9: Special Pythagorean triplet

https://projecteuler.net/problem=9

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.

分析

毕达哥拉斯三元组。求三个数a,b,c,满足a+b+c=1000, a2 + b2 = c2.

方法1 穷举法

思路很简单,a从1-1000遍历,b从 a 到 (1000-a) 遍历,判断 a2 + b2 == (1000-a-b)2 是否成立。

C\C++

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,a=0,b=0,c=0;

    for(i=1;i<1000;i++){
        for(j=i;j<1000-i;j++){
            if(i*i+j*j == (1000-i-j)*(1000-i-j)){
                a = i;b=j;c=1000-i-j;
                printf("a=%d,b=%d,c=%d,abc=%d \n",a,b,c,a*b*c);
                return 0;
            }
        }
    }

    return 0;
}

Golang

package main

import "fmt"

func main(){
    for i:=1; i<1000; i++ {
        for j:=i; j<1000-i; j++ {
            if i*i+j*j == (1000-i-j)*(1000-i-j) {
                // There exists exactly one Pythagorean triplet for which a + b + c = 1000.
                fmt.Printf("a=%d,b=%d,c=%d,abc=%d \n", i,j,1000-i-j, i * j * (1000-i-j) )
                return
            }
        }
    }
}

Python

# print a*b*c
print( [a * b * (1000-a-b) for a in range (1,1000) for b in range (a, 1000-a) if a**2 + b**2 == (1000-a-b)**2 ] )

# print a,b,c and a*b*c
#arr = [(a, b, (1000-a-b)) for a in range (1,1000) for b in range (a, 1000-a) if a**2 + b**2 == (1000-a-b)**2 ]
#print("a:", arr[0][0], "b:", arr[0][1], "c:", arr[0][2], "a*b*c:", arr[0][0] * arr[0][1] * arr[0][2])

答案

31875000

作者:JarvisChu
原文链接:Problem 9 Special Pythagorean triplet
版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0

发表评论