特殊的毕达哥拉斯三元组
作者:Gerhard R
https://projecteuler.net/problem=9
毕达哥拉斯三元组是指一组三个自然数,a < b < c,满足:
a^2 + b^2 = c^2
例如,3^2 + 4^2 = 9 + 16 = 25 = 5^2。
只存在一组毕达哥拉斯三元组满足 a + b + c = 1000。求出该组数的乘积 abc。
use v6; constant $N = 1000; my $result; 1..Int((1 - sqrt(0.5)) * $N) \ # compute numerator and denominator of closed expression for b ==> map -> $a { [ $a, $N * ($N - 2 * $a), 2 * ($N - $a) ] } \ # check if closed expression yields an integer ==> grep -> [ $a, $u, $v ] { $u %% $v } \ # compute b and c ==> map -> [ $a, $u, $v ] { my $b = $u div $v; [ $a, $b, $N - $a - $b ] } \ # compute product ==> map -> @triple { [*] @triple } \ # ... to give the result. # XXX Rakudo feed operator wraps results in an extra sequence, thus .[0] ==> { .[0].say }();