It is possible to show that the square root of two can be expressed as an infinite continued fraction.

 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...

By expanding this for the first four iterations, we get:

1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...

The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.

In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

题目大意:

2的平方根可以被表示为无限延伸的分数:

 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...

将其前四次迭代展开,我们得到:

1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...

接下来三次迭代的展开是99/70, 239/169, and 577/408, 但是第八次迭代的展开, 1393/985, 是第一个分子的位数超过分母的位数的例子。

在前1000次迭代的展开中,有多少个的分子位数超过分母位数?

//(Problem 57)Square root convergents
// Completed on Wed, 12 Feb 2014, 04:45
// Language: C
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/
#include<stdio.h> int add(int des[],int n1,int src[],int n2){
int i,f;
for(i= , f = ; i < n1 || i < n2 ; i++){
des[i] += ( f + src[i] ) ;
f = des[i]/ ;
des[i] %= ;
}
if(f)
des[i++] = f ;
return i;
}
int main(){
int num = ,sum = , k;
int array[][] = {} ;
int nn = ,dn = , f = ;//nn分子长度,dn分母长度,f分子位置 array[][] = ;
array[][] = ;
while(num<){
//分子加分母放到分子位置成为下一个分母
k = add(array[f],nn,array[-f],dn);
//分子加分母放到分母位置成为下一个分子
nn = add( array[-f],dn,array[f],k ) ;
dn = k ;
f = - f ;
if(nn > dn) sum++;
num++;
}
printf("%d\n",sum);
return ;
}
Answer:
153

(Problem 57)Square root convergents的更多相关文章

  1. Project Euler 57: Square root convergents

    五十七.平方根收敛(Square root convergents) 二的平方根可以表示为以下这个无穷连分数: \[ \sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac ...

  2. (Problem 46)Goldbach's other conjecture

    It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...

  3. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  4. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  5. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  6. (Problem 70)Totient permutation

    Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...

  7. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  8. (Problem 72)Counting fractions

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  9. (Problem 53)Combinatoric selections

    There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...

随机推荐

  1. BZOJ 1101 [POI2007]Zap(莫比乌斯反演)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1101 [题目大意] 求[1,n][1,m]内gcd=k的情况 [题解] 考虑求[1,n ...

  2. oralce dubugs

    1,The listener supports no services 2,invalid specification for system parameter LOCAL_LISTENER crea ...

  3. makefile简单helloworld

    最近要在unix系统上开发c++应用程序,但默认情况下unix编译c++程序需要使用makefile.其实makefile语法还是比较简单,看上去有点像ant.废话不说了,直接上helloworld. ...

  4. Oracle中NVARCHAR2与VARCHAR2的差别

    NVARCHAR2在计算长度时和字符集相关的: 比如数据库是中文字符集时以长度10为例, 1.NVARCHAR2(10)是能够存进去10个汉字的.假设用来存英文也仅仅能存10个字符. 2.而VARCH ...

  5. xcode 不值钱的动画UIButton

    #import "ViewController.h" @interface ViewController () /** 按钮 */ @property(nonatomic,weak ...

  6. js执行环境相关

    Js执行过程 如果一个文档中存在多个代码段 步骤一:读入第一个代码段(js引擎并非一行一行执行,而是一段一段分析执行) 步骤二:做词法分析和语法分析,有错则报语法错误(比如括号不匹配等),并跳转到步骤 ...

  7. PHP中类的继承关系

    在PHP中,我时常会写一个类,类写了一个共用方法,然后让子类去继承就能得到相应的功能.假设大致有这么一个父类: 1 <?php 2 class Father{ 3 4 public functi ...

  8. bzoj 1040: [ZJOI2008]骑士 树形dp

    题目链接 1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3054  Solved: 1162[Submit][S ...

  9. vs2013 cpu占用100%问题

    是由于显卡驱动支持wpf有问题 更新驱动或设置里取消自动调节视觉效果 http://support.microsoft.com/kb/2894215

  10. ForeignKey.on_delete

    当由一个 ForeignKey 引用的对象被删除,默认情况下,Django模拟SQL的 ON DELETE CASCADE 来删除对象的 ForeignKey 关系.这样可以覆盖指定的 on_dele ...