Problem 39
- 投稿者 : rei
Original
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}For which value of p 1000, is the number of solutions maximised?
和訳
辺の長さが{a,b,c}と整数の3つ組である直角三角形を考え, その周囲の長さをpとする. p = 120のときには3つの解が存在する:
{20,48,52}, {24,45,51}, {30,40,50}p < 1000 で解の数が最大になる p を求めよ.
当てにならないソースコード(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 25 26 27 28 29 30 31 32 33 34 | using System; namespace ProjectEuler { class Problem39 : Problem{ public Problem39() { int cntmax = 0, answer = 0; for (int p = 4; p < 1000; p++) { int cnt = CountTriangle(p); if (cnt > cntmax) { cntmax = cnt; answer = p; Console.WriteLine("{0}:{1}", p, cnt); } } Console.WriteLine("> " + answer); } int CountTriangle(int p) { int count = 0; for (int c = p / 3; c <p/2;c++ ) { int remain = p - c, cc = c * c; for (int a = 1; a<remain/2;a++ ) { int b = remain - a; if(cc == a*a+b*b){ ++count; } } } return count; } } } |
素直に解いても時間はかかりません。

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