project euler 94

Problem 94


Almost equilateral triangles

It is easily proved that no equilateral triangle exists with integral length sides and integral area. However, the almost equilateral triangle 5-5-6 has an area of 12 square units.

We shall define an almost equilateral triangle to be a triangle for which two sides are equal and the third differs by no more than one unit.

Find the sum of the perimeters of all almost equilateral triangles with integral side lengths and area and whose perimeters do not exceed one billion (1,000,000,000).


几乎等边的三角形

可以证明,不存在边长为整数的等边三角形其面积也是整数。但是,存在几乎等边的三角形 5-5-6,其面积恰好为12。

我们定义几乎等边的三角形是有两条边一样长,且第三边与这两边最多相差1的三角形。

对于所有边长为整数且周长不超过十亿(1,000,000,000)的三角形,求其中几乎等边的三角形的周长之和。

package projecteuler;

import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;

public class Prj94 extends TestCase {

	public static final int LIMIT = 1000000000;
	//public static final int LIMIT = 50;

	/**
	 * assume a = b = 2x ± 1, c = 2x; 
	 * area = sqrt[ p(p-a)(p-b)(p-c)] == m ===>
	 * (3x ± 2)^2 - 3m^2 = 1; 
	 * 2 + sqrt(3) is minimal solution
	 */
	public void testAlmostEquilateralTriangles() {

		SqrtDomain rt = new SqrtDomain(2, 1, 3);
		SqrtDomain lf = new SqrtDomain(2, 1, 3);

		List<SqrtDomain> dataList = new ArrayList<SqrtDomain>();
		dataList.add(rt.copyClone());

		for (int i = 1; i <= 100; i++) {
			lf = lf.multiply(rt);
			if (2 * lf.x > LIMIT + 1) {
				break;
			}
			dataList.add(lf.copyClone());
			System.out.println("i=" + i + "," + lf);
		}

		List<AlmostEquilateralTriangles> ret = new ArrayList<AlmostEquilateralTriangles>();
		for (int i = 0; i < dataList.size(); i++) {
			SqrtDomain sq = dataList.get(i);

			long val = 0;
			// a = 2x+1
			val = sq.x - 2;
			if (val > 0 && val % 3 == 0) {
				val = val / 3;
				if( 6 * val + 2 > LIMIT){
					continue;
				}
				AlmostEquilateralTriangles tp = new AlmostEquilateralTriangles(
						2 * val + 1, 2 * val + 1, 2 * val);
				ret.add(tp);
			}

			// a = 2x -1
			val = sq.x + 2;
			if (val > 0 && val % 3 == 0) {
				val = val / 3;
				if(6 * val - 2 > LIMIT){
					continue;
				}
				AlmostEquilateralTriangles tp = new AlmostEquilateralTriangles(
						2 * val - 1, 2 * val - 1, 2 * val);
				ret.add(tp);
			}

		}

		long sum = 0;
		for (int i = 0; i < ret.size(); i++) {
			AlmostEquilateralTriangles al = ret.get(i);
			sum += ( al.a + al.b + al.c);
			System.out.println(al);
		}

		System.out.println("sum=" + sum);

	}

	/**
	 * x + y *sqrt(n) 所有解为指数幂
	 * 
	 * @author suc
	 *
	 */
	public static class SqrtDomain {
		public long x;
		public long y;
		public int n;

		public SqrtDomain(long x, long y, int n) {
			super();
			this.x = x;
			this.y = y;
			this.n = n;
		}

		public SqrtDomain multiply(SqrtDomain rt) {
			long xx = x * rt.x + y * rt.y * n;
			long yy = x * rt.y + y * rt.x;
			int nn = n;
			return new SqrtDomain(xx, yy, nn);
		}

		public SqrtDomain copyClone() {
			return new SqrtDomain(x, y, n);
		}

		@Override
		public String toString() {
			return "SqrtDomain [x=" + x + ", y=" + y + "]";
		}
	}

	public static class AlmostEquilateralTriangles {
		public long a;
		public long b;
		public long c;

		public AlmostEquilateralTriangles(long l, long m, long n) {
			this.a = l;
			this.b = m;
			this.c = n;
		}

		@Override
		public String toString() {
			return "AlmostEquilateralTriangles [a=" + a + ", b=" + b + ", c="
					+ c + "]";
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值