码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-26 01:08:52      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:bottom   hid   set   esb   size   tle   mono   data   product   

 

[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

标签:bottom   hid   set   esb   size   tle   mono   data   product   

原文地址:http://www.cnblogs.com/wander4096/p/7594632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!