欧拉工程第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 ...
随机推荐
- Linux 本地yum源搭建和网络yum源搭建
一.本地yum源搭建 首先挂载上光盘 [root@www /]# mount /dev/cdrom /media/cdrom/ 系统默认已经安装了可使用yum的软件包,所以可以直接配置: [root@ ...
- elr_memory_pool详解
Preface Usually, memory allocation of OS is fast, especially the computer has just started. But over ...
- 在Linux中三种让crontab每秒执行任务的方法
第一种方法: 1.创建脚本文件 cat phplog.sh 2.编辑脚本内容 #!/bin/bash while : ;do /home/scripts.sh 2>/dev/null & ...
- Sublime Text 前端开发常用扩展插件推荐
Sublime Text 前端开发常用扩展插件推荐 Sublime Text Sublime Text 是程序员们公认的编码神奇,拥有漂亮的用户界面和强大的功能 更重要的是,Sublime Text ...
- Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view
解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...
- delphi图形图像开发相关
①delphi的图形处理(doc) http://wenku.baidu.com/view/519df09951e79b89680226ee.html ②delphi的图形图像处理(ppt) http ...
- EF-Code First 入门
本文程序基于VS2015.EF6.1,本文不做过多深入讨论,只是个入门. EF 就是微软的 EntityFramework,主要分为 DB First,Model First,Code First.之 ...
- WebDev.WebServer40.exe已停止工作
今天写程序的遇到这个错误 错误的原因是代码中有死循环
- Redis 四:存储类型之无序集合
.sadd num a b c 向num集合中添加abc三个元素 .srem num b 从num集合中删除b元素 .smembers num 获取num集合中所有的元素 .sismember num ...
- java & xml parser
参考: JDK8 API: http://docs.oracle.com/javase/8/docs/api/ DOM: http://www.w3.org/TR/2004/REC-DOM-Level ...