Project Euler Problem 75
2015-10-13 00:00
351 查看
It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that L is the length of the wire, for how many values of L 1,500,000 can exactly one integer sided right angle triangle be formed?
Note: This problem has been changed recently, please check that you are using the right parameters.
下面是java解题方法:
这个解法的时间在秒级,解法利用了下面勾股数的性质:
1,通过a = m^2 − n^2, b = 2mn, c = m^2 + n^2 (1)可以生成所有的素勾股数和一部分派生勾股数(a,b,c),其在(m>n,m,n为正整数);
2,勾股数分为素勾股数和派生勾股数,而且所有派生勾股数都是由素勾股数派生而来的;
3,根据1,2,利用式(1),遍历m,n并加上派生规则可以生成所有勾股数。
注:正整数a,b,c满足a^2+b^2=c^2<=>(a,b,c)是勾股数,如果(a,b,c)=1(即a,b,c互素),则(a,b,c)称为素勾股数,对于整数k,(ka,kb,kc)是由(a,b,c)派生的勾股数。
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that L is the length of the wire, for how many values of L 1,500,000 can exactly one integer sided right angle triangle be formed?
Note: This problem has been changed recently, please check that you are using the right parameters.
下面是java解题方法:
public static void main(String[] args) { Map<Integer, ABC> dict = new HashMap<Integer, ABC>(); int L = 1500000; int M = (int)Math.sqrt(L / 2); for (int m = 1; m <= M; m++) { for (int n = 1; n < m; n++) { int a = m * m - n * n; int b = 2 * m * n; int c = m * m + n * n; int l = a + b + c; int k = L / l; for (int i = 1; i <= k; i++) { int[] abc = { i * a, i * b, i * c }; Arrays.sort(abc); check(i, l, abc, dict); } } } int count = 0; for (ABC abc : dict.values()) { count += abc.got; } System.out.println(count); } private static void check(int i, int l, int[] abc, Map<Integer, ABC> dict) { int ll = i * l; ABC tmp = dict.get(ll); if (tmp != null) { if (tmp.got == 1) { int[] tmp1 = tmp.abc; for (int x = 0; x < 3; x++) { if (tmp1[x] != abc[x]) { tmp.got = 0; break; } } } } else { dict.put(ll, new ABC(abc)); } } static class ABC { public ABC(int[] abc) { this.abc = abc; } int got = 1; int[] abc; }
这个解法的时间在秒级,解法利用了下面勾股数的性质:
1,通过a = m^2 − n^2, b = 2mn, c = m^2 + n^2 (1)可以生成所有的素勾股数和一部分派生勾股数(a,b,c),其在(m>n,m,n为正整数);
2,勾股数分为素勾股数和派生勾股数,而且所有派生勾股数都是由素勾股数派生而来的;
3,根据1,2,利用式(1),遍历m,n并加上派生规则可以生成所有勾股数。
注:正整数a,b,c满足a^2+b^2=c^2<=>(a,b,c)是勾股数,如果(a,b,c)=1(即a,b,c互素),则(a,b,c)称为素勾股数,对于整数k,(ka,kb,kc)是由(a,b,c)派生的勾股数。
相关文章推荐
- MyEclipse Web Project转Eclipse Dynamic Web Project
- Notice: Trying to get property of non-object problem(PHP)解决办法
- 解决VS2012 Express的There was a problem sending the command to the program问题
- 代码规范及CodeReview要点
- RDM 与Project 的对比分析报告
- Project Server 2010安装和配置攻略
- Struts2 Problem Report: No result defined for action ... and result exception
- some AJAX Project
- ProjectWise 工程内容管理及协同工作解决方案系列网络讲座
- 你的Exchange Server 管理员已经封锁您所使用的OUTLOO
- Excel- There was a problem sending the command to the program
- 解决 You could try using --skip-broken to work around the problem
- How to fix the USB root hub power management issue in Windows 7
- Console access Issue
- idea中构建maven项目,Generating project in Batch mode 慢
- Project facet Java version 5.0 is not supported.
- Ubuntu每次启动都显示System program problem detected的解决办法
- Ubuntu—12.04 无法更新,老是提示系统出问题,更新缓存失败E:Encountered...
- 菜鸟的Android之路-02《eclipse新建android project》
- perl: warning: Setting locale failed.