鱼C论坛

 找回密码
 立即注册
查看: 3941|回复: 12

题目65:找出e的连分数中第100个收敛项的分子各位之和。

[复制链接]
发表于 2015-8-8 01:36:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Convergents of e

The square root of 2 can be written as an infinite continued fraction.

QQ20150808-1@2x.png

The infinite continued fraction can be written, QQ20150808-5@2x.png = [1;(2)], (2) indicates that 2 repeats ad infinitum. In a similar way, QQ20150808-6@2x.png = [4;(1,3,1,8)].

It turns out that the sequence of partial values of continued fractions for square roots provide the best rational approximations. Let us consider the convergents for QQ20150808-5@2x.png .

QQ20150808-2@2x.png

Hence the sequence of the first ten convergents for QQ20150808-5@2x.png are:

1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, ...
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 QQ20150808-3@2x.png convergent is 1+4+5+7=17.

Find the sum of digits in the numerator of the QQ20150808-4@2x.png convergent of the continued fraction for e.

题目:

2的平方根可以写作无限连分数:

QQ20150808-1@2x.png

这个无限连分数可以写作, QQ20150808-5@2x.png = [1;(2)], (2) 表示2无限循环出现。类似的, QQ20150808-6@2x.png = [4;(1,3,1,8)]。

事实证明平方根的连分数序列提供了最好的有理数近似值。让我们考虑 QQ20150808-5@2x.png 的收敛项:

QQ20150808-2@2x.png

因此 QQ20150808-5@2x.png 的收敛项中的前十项是:

1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, ...

更令人惊奇的是一个重要的数学常数:

e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].

e 的收敛项序列中的前十项是:

2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ...

其中第十项的分子各位数之和是 1+4+5+7=17。

找出 e 的收敛项序列中第 100 项的分子上各位之和。



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-24 14:09:40 | 显示全部楼层
  1. 272
  2. [Finished in 0.1s]
复制代码


本来写了递归的算法,结果run了半天出不来,果然递归的效率太低,用列表就快多了
  1. k = []
  2. for i in range(34):
  3.         k.append (1)
  4.         k.append (2*i+2)
  5.         k.append (1)

  6. En = [2,3]
  7. for j in range(2,101):
  8.         En.append (En[j-1] * k[j-1] + En[j-2])

  9. E100 = str(En[99])
  10. total = 0
  11. for each in E100:
  12.         total += int(each)
  13. print (total)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-20 19:30:58 | 显示全部楼层
  1. def con_e(num):#将连分数e的前num项组成list
  2.     e=[2]
  3.     for i in range(1,num):
  4.         if i%3==2:
  5.             e.append(2*i//3+1)
  6.         else:
  7.             e.append(1)
  8.     return e

  9. def deno(num):#将c+1/(b/a)转为b/a的格式,迭代c,最后一项不倒置,所以a即num项的分子
  10.     e=con_e(num)
  11.     a=e.pop()
  12.     b=1
  13.     while len(e):
  14.         c=e.pop()
  15.         temp=a*c+b
  16.         b=a
  17.         a=temp
  18.     return a

  19. num=100
  20. print(sum(int(i) for i in str(deno(num))))
复制代码

272
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-3 18:12:43 | 显示全部楼层
用matlab算这个题目就是艹蛋,放弃
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-19 10:58:52 | 显示全部楼层
  1. from fractions import Fraction

  2. def fun(n):
  3.   if n == 0:
  4.     return 2
  5.   elif n % 3 == 2:
  6.     return (n + 1) * 2 // 3
  7.   else:
  8.     return 1

  9. ans = Fraction(0)
  10. for i in map(lambda x: fun(x), reversed(range(100))):
  11.   ans += i
  12.   ans = 1 / ans

  13. print(sum(map(lambda x: int(x), str(ans.denominator))))
复制代码


https://github.com/devinizz/project_euler/blob/master/page02/65.py
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 15:18:56 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:47 编辑
  1. n=99;
  2. k=ones(1,n);
  3. k(2:3:end)=2*(1:round(n/3));
  4. k=flip(k);
  5. fenzi=sym('1');
  6. fenmu=sym(k(1));
  7. for i=2:n
  8.     temp=sym(fenmu);
  9.     fenmu=sym(fenzi+k(i)*temp);
  10.     fenzi=temp;
  11. end
  12. total=[fenzi+2*fenmu fenmu];
  13. t1=char(total(1));
  14. wei=length(t1);
  15. s=0;
  16. for j=1:wei
  17.     s=s+str2num(t1(j));
  18. end
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 16:17:40 | 显示全部楼层
j.p.-hwang 发表于 2020-6-28 15:18
n=99;
k=ones(1,n);
k(2:3:end)=2*(1:round(n/3));

这……是什么语言
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-28 22:13:30 | 显示全部楼层
272

Process returned 0 (0x0)   execution time : 0.032 s
Press any key to continue.
使用栈储存数据,递推计算,大数
  1. #include<iostream>
  2. #include<vector>
  3. #include<stack>
  4. using namespace std;

  5. typedef struct BigInt{
  6.   vector<int> v;

  7.   BigInt(vector<int> v):v(v){}
  8. }BI;

  9. BI operator + (const BI & a,const BI & x){
  10.   vector<int> t1 = a.v;
  11.   vector<int> t2 = x.v;
  12.   if (a.v.size() < x.v.size())  swap(t1,t2);

  13.   for (int i = 0;i < max(t1.size(),t2.size() );i++){
  14.       if (i < t2.size())  t1[i] += t2[i];
  15.       if (i > 0 && t1[i-1] > 9)  {t1[i]++;  t1[i-1] -= 10;}
  16.   }

  17.   if (t1[t2.size()-1] > 9)
  18.     if (t1.size() == t2.size() )  {t1.push_back(1);  t1[t1.size()-2] -= 10;}

  19.     else {
  20.       t1[t2.size()]++;
  21.       for (int i = t2.size();i < t1.size()-1;i++){
  22.         if (t1[i] > 9) {t1[i] -= 10;  t1[i+1]++;}
  23.         else break;
  24.       }
  25.       t1[t2.size()-1] -= 10;
  26.     }
  27.   return BI(t1);
  28. }

  29. BI operator * (const int c,const BI & x){
  30.   BI res = x;

  31.   for (int i = 0;i < res.v.size();i++)
  32.     res.v[i] *= c;

  33.   for (int i = 0;i < res.v.size()-1;i++)
  34.     if (res.v[i] > 9) {
  35.       res.v[i+1] += res.v[i] / 10;
  36.       res.v[i] %= 10;
  37.   }
  38.   int sz = res.v.size();
  39.   if (res.v[sz-1] > 9)  {res.v.push_back(res.v[sz-1] / 10); res.v[sz-1] %= 10;}

  40.   return res;
  41. }

  42. void print_ans(const BI & x){
  43.   int ans = 0;

  44.   for (int i = 0;i < x.v.size();i++)
  45.     ans += x.v[i];

  46.   cout << ans <<endl;
  47. }

  48. int main()
  49. {
  50.   vector<int> a(1,1),b(1,1);
  51.   BI n(a),d(b);
  52.   stack<int> s;

  53.   for (int i = 1;i < 99;i++){
  54.     int k = i/3 + 1;

  55.     if (i % 3 == 2) s.push(2*k);
  56.     else  s.push(1);
  57.   }
  58.   while(s.size() ){//先算e-2,再补回去
  59.     //cout << s.top() << endl;
  60.     BI t = d;
  61.     d = n + s.top() * d;
  62.     n = t;

  63.     s.pop();
  64.   }
  65.   n = n + 2*d;

  66.   print_ans(n);
  67.   return 0;
  68. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-21 12:04:10 | 显示全部楼层
  1. /*
  2. 答案:272
  3. */
  4. #include <iostream>
  5. #include <string>
  6. #include <gmpxx.h>
  7. using namespace std;

  8. int main(void)
  9. {
  10.   mpz_class p1 = 2, p2 = 3, p3, q1 = 1, q2 = 1, q3;
  11.   for (int i = 3; i <= 100; ++i)
  12.   {
  13.     if (i % 3 == 0)
  14.     {
  15.       p3 = p2 * 2 * (i / 3) + p1;
  16.       q3 = q2 * 2 * (i / 3) + q1;
  17.     }
  18.     else
  19.     {
  20.       p3 = p2 + p1;
  21.       q3 = q2 + q1;
  22.     }
  23.     p1 = p2;
  24.     q1 = q2;
  25.     p2 = p3;
  26.     q2 = q3;
  27.   }
  28.   string str = p2.get_str(10);
  29.   int nSum = 0;
  30.   for(int i = 0; i < int(str.length()); ++i)
  31.     nSum += int(str[i] - 48);
  32.   cout << nSum << endl;
  33.   return 0;
  34. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-27 10:56:17 | 显示全部楼层
  1. def generator():
  2.     yield 2
  3.     k = 2

  4.     while True:
  5.         yield 1
  6.         yield k
  7.         yield 1
  8.         k += 2

  9. it = [i for i, _ in zip(generator(), range(100))]
  10. it.reverse()
  11. it = iter(it)

  12. nume = next(it)
  13. deno = 1

  14. for i in it:
  15.     nume, deno = i * nume + deno, nume

  16. print(sum(int(i) for i in str(nume)))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-10 20:50:16 | 显示全部楼层
本题可以应用连分数的递推公式和大数库mpir(或gmp)来解。
  1. /*
  2. 答案:272
  3. */
  4. #include <iostream>
  5. #include <string>
  6. #include <mpirxx.h>
  7. using namespace std;

  8. int main(void)
  9. {
  10.   mpz_class p1=2, p2=3, p3;
  11.   mpz_class q1=1, q2=1, q3;
  12.   for (int i = 3; i <= 100; ++i)
  13.   {
  14.     int a = 1;
  15.     if (i % 3 == 0)
  16.       a = (i / 3) * 2;
  17.     p3 = a*p2 + p1;
  18.     q3 = a*q2 + q1;
  19.     p1 = p2;
  20.     p2 = p3;
  21.     q1 = q2;
  22.     q2 = q3;
  23.   }
  24.   string str = p3.get_str();
  25.   int nSum = 0;
  26.   for (int i = 0; i < (int)str.length(); ++i)
  27.     nSum += int(str[i] - '0');
  28.   cout << nSum << endl;
  29.   return 0;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-29 22:24:20 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()
  3. decimal_list = []
  4. for n in range(2, 101):
  5.     if not n % 3:
  6.         decimal_list.append(n // 3 * 2)
  7.     else:
  8.         decimal_list.append(1)

  9. numerator = 1
  10. denominator = decimal_list[-1]
  11. for n in range(98, 0, -1):
  12.     numerator, denominator = denominator, numerator + denominator * decimal_list[n - 1]
  13. numerator += denominator * 2
  14. print(sum([int(i) for i in str(numerator)]))
  15. print("It costs %f s" % (t.perf_counter() - start))
复制代码


272
It costs 0.000073 s
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-27 20:11:06 | 显示全部楼层

matlab,虽然已经三年了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-11 16:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表