[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

?

时间: 2024-11-10 12:11:20

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet的相关文章

Project Euler: 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. 首先联立式子,消除c,得到关于ab的等式5

Special Pythagorean triplet

problem 9:Special Pythagorean triplet 题意:若a+b+c=1000,且a2+b2=c2,求abc 代码如下: 1 #ifndef PRO9_H_INCLUDED 2 #define PRO9_H_INCLUDED 3 4 int solve(){ 5 for(int i=1;i<=1000;++i) 6 for(int j=1;i+j<1000;++j) 7 if(i*i+j*j==(1000-i-j)*(1000-i-j)) 8 return i*j*(

Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验

Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shall call it a special sum set if for any two non-empty disjoint subsets, B and C, the following properties are true: S(B) ≠ S(C); that is, sums of subse

Project Euler: Problem 14 Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can b

Project Euler problem 68

题意需要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.并且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下就行. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctime> #include <set> #i

Project Euler problem 69

考察欧拉函数的一道题 首先要知道 [定理]正整数n(n≥2)可以唯一分解成素数乘积,即:n =p[1]^r1 * p[2] ^r2 * p[3]^r3. *...* p[s]^rs 其次欧拉函数有两个性质,可以用来编程,单独求phi函数: ① phi(m) =  m ( 1- 1/p[1]) ( 1- 1/p[2])-( 1- 1/p[s]) ② phi(p^k) = p^k – p^(k-1) = (p-1)p^(k-1) 然后 m/phi(m)  = (p[1] *p[2]*p[3]*

Project Euler:Problem 12 Highly divisible triangular number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the fir

Project Euler:Problem 11 Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30

Project Euler: Problem 17 Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be