您的位置:首页 > 其它

project euler 100

2015-12-23 22:18 302 查看


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 + "]";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: