project euler 75

Problem 75


Singular integer right triangles

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?


唯一的整数边直角三角形

只能唯一地弯折成整数边直角三角形的电线最短长度是12厘米;当然,还有很多长度的电线都只能唯一地弯折成整数边直角三角形,例如:

12厘米: (3,4,5)
24厘米: (6,8,10)
30厘米: (5,12,13)
36厘米: (9,12,15)
40厘米: (8,15,17)
48厘米: (12,16,20)

相反地,有些长度的电线,比如20厘米,不可能弯折成任何整数边直角三角形,而另一些长度则有多个解;例如,120厘米的电线可以弯折成三个不同的整数边直角三角形。

120厘米: (30,40,50), (20,48,52), (24,45,51)

记电线长度为L,对于L ≤ 1,500,000,有多少种取值只能唯一地弯折成整数边直角三角形?

package projecteuler;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import junit.framework.TestCase;

public class Prj75 extends TestCase{

	private static final int UP_LIMIT = 1500000;

	
	/**
	 * a = k * ( 2mn);
	 * b = k * ( mm + nn);
	 * c = k * ( mm - nn);
	 * b + c < p ===> m < sqrt( p/ 2);
	 * 
	 * (m,n) == 1;
	 * 
	 */
	public void testSingularIntegerRightTriangles(){
		
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		
		int p = UP_LIMIT;
		for( int m = 2; m <= Math.sqrt(p/2); m ++){
			
			
			for(int n = 1; n < m; n ++){
				
				if( gcd(m, n) == 1 && ( m- n) % 2 != 0){
					for( int k = 2 * m * ( m + n); k < p; k = k + 2 * m * (m + n)){
						if( !map.containsKey(k)){
							map.put(k, 0);
						}
						map.put(k, map.get(k) + 1);
					}
				}
			}
		}
		
		int count = 0;
		for( Entry<Integer, Integer> entry : map.entrySet()){
			
			if( entry.getValue() == 1){
				count ++;
			}
		}
		
		System.out.println("count=" + count);
	}
	
	int gcd(int m, int n) {

		int a = m;
		int b = n;
		if (m < n) {
			a = n;
			b = m;
		}

		int t = 0;
		while (a % b != 0) {
			t = a % b;
			a = b;
			b = t;
		}
		return b;

	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值