欧拉工程第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 ...
随机推荐
- 打造简单实用的Thinkphp分页样式(Bootstrap版本)
先吐槽一下ThinkPHP3.1版的分页样式,虽然看起来也很简单大方,但是所有的页码全是使用简单的数字,之间的空隙比较小,不大容易点,还有那个“前5页”和“后5页”显得有点多余,因为点击当前显示第一页 ...
- js 倒计时 倒计时60秒
<input type="button" id="btn" value="免费获取验证码" onclick="settime ...
- OpenSSL 安全漏洞: heartbleed
Heartbleed 是 2014年4月7日被广泛报道的一个 OpenSSL 安全漏洞,号称是灾难. 利用它能读取服务器上最多64k的内存,只要该服务器可以通过ssl连接. Heartbleed ...
- 【Qt】Qt Linguist介绍【转】
简介 Qt提供了一款优秀的支持Qt C++和Qt Quick应用程序的翻译工具.发布者.翻译者和开发者可以使用这款工具来完成他们的任务. 发布者:承担了全面发布应用程序的责任.通常,他们协调开发者和翻 ...
- eclipse juno版本中没用 ant
下载了谷歌提供的Android集成开发工具ADT,里面封装了Eclipse,但是很奇怪的是竟然没有Ant插件在里面 标准的Eclipse一般都是内置集成了Ant的. 然后到eclipse的plugin ...
- phpStudy for Linux (lnmp+lamp一键安装包)
phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 phpStudy for Linux 支持Apache/Ng ...
- cadence 16.6 Pspice 仿真步骤
从ADI官网下载后缀为 cir 的文件,AD8210 为例 进行仿真 1 打开 Cadence -> Release 16.6 -> PSpice Accessories -> Mo ...
- constructor(构造器)
当我们创建一个类的时候,如果不自定义构造器,则系统会自动创建一个默认的构造器,也是一个无参构造器用于初始化. 当我们在类的里面创建了自己的构造器,则系统将不会创建默认的构造器,由于需求条件不同,构造器 ...
- WWDC 2016: Rich Notifications in iOS 10
Notifications have gotten more than a visual refresh in iOS 10. As part of the new UserNotifications ...
- [转载]非常完善的Log4net详细说明
前言 此篇文章是我见过写得最好的一片关于Log4Net的文章,内容由简入难,而且面面俱到,堪称入门和精通的佳作,特从懒惰的肥兔的转载过来. 1.概述 log4net是.Net下一个非常优秀的开源日志记 ...