project euler 100

Problem 100


Arranged probability

If a box contains twenty-one coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, it can be seen that the probability of taking two blue discs, P(BB) = (15/21)×(14/20) = 1/2.

The next such arrangement, for which there is exactly 50% chance of taking two blue discs at random, is a box containing eighty-five blue discs and thirty-five red discs.

By finding the first arrangement to contain over 1012 = 1,000,000,000,000 discs in total, determine the number of blue discs that the box would contain.


安排概率

在一个盒子中装有21个彩色碟子,其中15个是蓝的,6个是红的。如果随机地从盒子中取出两个碟子,取出两个蓝色碟子的概率是P(BB) = (15/21)×(14/20) = 1/2。

下一组使得取出两个蓝色盘子的概率恰好为50%的安排,是在盒子中装有85个蓝色碟子和35个红色碟子。

当盒子中装有超过1012 = 1,000,000,000,000个碟子时,找出第一组满足上述要求的安排,并求此时盒子中蓝色碟子的数量。

package projecteuler;

import junit.framework.TestCase;


/**
 * http://mathworld.wolfram.com/PellEquation.html
 * @author suc
 *
 */
public class Prj100 extends TestCase{

	
	public static final long LIMIT = 1000000000000L;
	
	/**
	 * B(B-1)/(m(m-1) = 1/2===>
	 * (2m-1)^2 - 2(2B-1)^2= -1;
	 */
	public void testArrangedProbability(){
		SqrtDomain rt = new SqrtDomain(1, 1, 2);
		SqrtDomain lf = new SqrtDomain(1, 1, 2);

		for (int i = 1; i <= 100; i++) {
			lf = lf.multiply(rt);

			if( lf.x > LIMIT * 2 - 1L && lf.x % 2 != 0 && lf.y % 2 != 0){
				System.out.println("result=" + i + "," + lf);
				System.out.println("blue=" + (( lf.y + 1) / 2));
				return;
			}
			System.out.println("i=" + i + "," + lf);
		}
	}
	
	
	/**
	 * 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 + "]";
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值