project euler 100
2015-12-23 22:18
302 查看
Problem
100
Arranged probabilityIf 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 + "]"; } } }
相关文章推荐
- Java方法参数是引用调用还是值调用?
- 【翻译自mos文章】当/var/tmp目录被remove掉之后,GI crash,并启动失败,原因是ohasd can not create named pipe
- apache rewrite重写配置和本机域名配置,访问权限设置,伪静态实际应用
- iOS开发之绘制文本内容
- 数据结构实验之排序四:寻找大富翁
- 每日一篇:简单完全平方数
- 对将关系模式分解为高级范式的认识
- 即将被裁掉,她是如何替他力挽狂澜?
- MAC上安装mySQL5.7后密码过期处理
- 哔了狗的省市区
- linux 下安装WebBench进行web 性能测试
- Eclipse、SDK、ADT资源下载
- 测试类 模拟http请求 运用Jodd包
- Android之实时监控网络状态
- java虚拟机垃圾回收机制
- 岁月划过生命线——大一下
- Oracle_数据库主机oracle账号被删除
- 从关系型数据库到非关系型数据库
- qt 5.5.1 连接 mysql 5.7.9
- Linux使用RPM安装软件