欧拉工程第65题:Convergents of e
现在做这个题目真是千万只草泥马在心中路过
这个与上面一题差不多
这个题目是求e的第100个分数表达式中分子的各位数之和
What is most surprising is that the important mathematical constant,
e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].
The first ten terms in the sequence of convergents for e are:
2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ...
The sum of digits in the numerator of the 10th convergent is 1+4+5+7=17.
Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.
上面可以发现一个规律
维基百科链接:连分数,上面有递推公式
这么多就足够解题了,
上面的定理1,用不到的
e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].
a0=2
下面的:1,2,1,1,4,1,1,6,1,这个规律很明显
根据上面的规律
Java代码:
package project61;
import java.math.BigInteger;
public class P65{
void run(){
BigInteger d = new BigInteger("1");
BigInteger n = new BigInteger("2");
for(int i= 2;i<=100;i++){
BigInteger temp = d;
long c = (i%3==0)?2*(i/3):1;
BigInteger BigC = new BigInteger(c+"");
d = n;
n = d.multiply(BigC).add(temp);
}
String toStr = n.toString();
int result = 0;
for(int i=0;i<toStr.length();i++){
result += Integer.valueOf(toStr.charAt(i)+"");
}
System.out.println(toStr+"\nresult:"+result);
}
public static void main(String[] args){
long start = System.currentTimeMillis();
new P65().run();
long end = System.currentTimeMillis();
long time = end - start;
System.out.println("run time:"+time/1000+"s"+time%1000+"ms");
}
}
如果刚看到这一题的时候应该感觉这个数不是很大,然而分子是:6963524437876961749120273824619538346438023188214475670667
分子各位的数字和是:272,要用BigInteger类型
Python程序:
import time as time def mysum(num):
return sum(map(int,str(num))) def getA(i):
if i%3==0:
return 2*(i/3)
else:
return 1 def run():
h0 = 1
h1 = 2
for i in range(2,101):
a = getA(i)
h2 = a * h1 + h0
h0 = h1
h1 = h2
return mysum(h2) if __name__== '__main__':
start = time.time()
result = run()
print "running time={0},result={1}".format((time.time()-start),result)
Python是根据上面截图中的写的
running time=0.0,result=272
欧拉工程第65题:Convergents of e的更多相关文章
- 欧拉工程第69题:Totient maximum
题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第57题:Square root convergents
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; impo ...
- 欧拉工程第67题:Maximum path sum II
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
- 欧拉工程第55题:Lychrel numbers
package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...
- 欧拉工程第54题:Poker hands
package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...
- 欧拉工程第53题:Combinatoric selections
package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...
随机推荐
- 转:java中volatile关键字的含义
转:java中volatile关键字的含义 在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言 ...
- 关于Silverlight调用天气预报接口问题
问题:因Silverlight客户端不能直接调用webservice接口(外网天气接口),调用会出现跨域访问的问题,即使添加了跨域文件也不好使.解决方法如下 解决方法一:1.在服务端建立一个wcf服务 ...
- silverlight 文本框只能输入数字
void mobile_KeyUp(object sender, KeyEventArgs e) { Regex rg = new Regex("^[0-9]{1,11}$"); ...
- Translation001——android
请尊重原创,转载请注明出处: Author:KillerLegend Link:http://www.cnblogs.com/killerlegend/ BEGIN****************** ...
- Firebird数据库相关备忘录
Firebird数据库中有一些很特别的东西,很好用,但由于平时用的不多,记在这里,以备以后用到时查询. 1.以ADO 的OLE ODBC驱动方式访问 Firebird,可以使用如下连接串: FBCon ...
- selenium for python 所有方法
先列出selenium所有方法,然后挨个使用!说明 add_cookieapplication_cachebackcapabilitiesclosecommand_executorcreate_web ...
- Configure Log Shipping
准备工作 两台装有的Windows Server 2012R2以及SQL Server 2012的服务器 下载评估版 Windows Server 2012 R2 下载 Microsoft SQL S ...
- Hive表分区
必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...
- 使用IE浏览器下载时候窗口一闪而过
使用IE浏览器下载东西时,窗口一闪而过,那么这个问题怎么处理呢? 解决办法: 1.按住ctrl键进行下载 2.浏览器>工具>internet选项>安全自定义级别>下载文件自动提 ...
- input 标签的监听事件总结
最近在写一个手机端提交表单的项目,里面用了不少input标签,因为项目不太忙,所以,想做的完美点,但是遇到了一些问题,比如:页面中的必填项如果有至少一项为空,提交按钮就是不能提交的状态,所以需要对所有 ...