Problem 9
- 投稿者 : rei
Original
A Pythagorean triplet is a set of three natural numbers, a b c, for which,
a² + b² = c²
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²
を満たす数の組である.例えば, 3² + 4² = 9 + 16 = 25 = 5²である.
a + b + c = 1000となるピタゴラスの三つ組が一つだけ存在する. このa,b,cの積を計算しなさい.
当てにならないソースコード(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; namespace ProjectEuler { class Problem9 { const int target = 1000; public Problem9() { int a = 0, b = 0, c = (int)Math.Ceiling((double)target / 3), answer = 0; Console.WriteLine(this.ToString()); for (; c < (target>>1);c++) { for (a = c - 1; ; a--) { b = target - a - c; if (b == c) break; if (a * a + b * b == c * c) { answer = a*b*c; } } } Console.WriteLine("> " + answer); } } } } |
a<b<c;、a+b>cから333<c<500は確実なのですが、
それをコードにすると見にくいですね。

rei@sikios.com
コメントはまだありません。