[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet相关的知识,希望对你有一定的参考价值。

 

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

Description

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

a^2 + b^2 = c^2

For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

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

Code

constant n = 1000;
constant sup = Int((1 - sqrt(0.5)) * n);

say [*] flat gather for 1..sup -> \\a {
    my \\u = n * (n - 2 * a);
    my \\v = 2 * (n - a);
    take a, u / v, n - a - u / v if u %% v;
}

Explanation

(0) n = 1000

(1) a < b < c

(2) a + b + c = n

(3) a^2 + b^2 = c^2

(4) b = n(n - 2a) / 2(n - a)

(5) c = n(n - 2a) / 2(n - a) + a^2 / (n - a) = n - a - b

?

以上是关于[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet的主要内容,如果未能解决你的问题,请参考以下文章

project euler做题记录

project euler 169

C++ 似乎比 Project Euler 的 Python Ruby 慢得多

欧拉计划(Euler Project)——第一题到第三题

Python练习题 034:Project Euler 006:和平方与平方和之差

Python Project Euler数字第五种力量