|
Q9:Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.
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.
单就做这个题,也很easy:
SQL> with t as (select rownum n from dual connect by rownum<=1000/2)
2 select ta.n a, tb.n b, 1000-ta.n-tb.n c, ta.n * tb.n * (1000-ta.n-tb.n) prod from t ta, t tb
3 where ta.n<tb.n and
4 ta.n* ta.n + tb.n*tb.n = (1000-ta.n - tb.n)*(1000-ta.n - tb.n)
5 /
A B C PROD
---------- ---------- ---------- ----------
200 375 425 31875000
Executed in 0.297 seconds
不过,毕竟这是个O(n2)的算法,推广到大数以后,就是个问题了。
Euler的文档中有降低难度的算法, 不过看不明白。
[ 本帖最后由 tree_new_bee 于 2010-12-15 19:20 编辑 ] |
|