Project Euler Problem 39
問題
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?
ソース
p (3..1000).map{|l| count = 0 1.upto(l / 3){|a| a.upto((l - a) / 2){|b| c = l - a - b count += 1 if a * a + b * b == c * c } } [l, count] }.sort{|a, b| b[1] <=> a[1]}[0][0]
解答
840
感想
a, b, cの組み合わせさえ出来ればあとは簡単!
長さと解の数を配列で返し、ソートしているのが特徴です。