zoukankan      html  css  js  c++  java
  • 欧拉工程第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.

     

    上面可以发现一个规律

     

    维基百科链接:连分数,上面有递推公式

     

    image

     

    这么多就足够解题了,

     

      上面的定理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+"
    result:"+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
  • 相关阅读:
    二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment
    思维题 HDOJ 5288 OO’s Sequence
    树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
    最大流增广路(KM算法) HDOJ 1853 Cyclic Tour
    最大流增广路(KM算法) HDOJ 1533 Going Home
    最大流增广路(KM算法) HDOJ 2255 奔小康赚大钱
    Complete the Word CodeForces
    Gadgets for dollars and pounds CodeForces
    Vasya and Basketball CodeForces
    Carries SCU
  • 原文地址:https://www.cnblogs.com/bbbblog/p/4786903.html
Copyright © 2011-2022 走看看