(Problem 57)Square root convergents
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的更多相关文章
- Project Euler 57: Square root convergents
五十七.平方根收敛(Square root convergents) 二的平方根可以表示为以下这个无穷连分数: \[ \sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac ...
- (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 ...
- (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 ...
- (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 ...
- (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 ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
- (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 ...
- (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 ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
随机推荐
- tarjan算法大意
Tarjan算法 (以发现者Robert Tarjan命名)是一个在图中寻找强连通分量的算法.算法的基本思想为:任选一结点开始进行深度优先搜索dfs(若深度优先搜索结束后仍有未访问的结点,则再从中任选 ...
- SGU 200 Cracking RSA (高斯消元)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出m个整理,因子全部为前t个素数.问有多少 ...
- hdu 2519 新生晚会 排列组合
通过阶段性计算减少一次性的大值计算 #include <stdio.h> int main() { int t, a, b, i; __int64 c; scanf("%d&qu ...
- 寻找数列中第k大的数算法分析
问题描述:给定一系列数{a1,a2,...,an},这些数无序的,现在求第k大的数. 看到这个问题,首先想到的是先排序,然后直接输出第k大的数,于是得到啦基于排序的算法 算法一: #include&l ...
- Java关键字static
链接地址:http://www.cnblogs.com/devinzhang/archive/2011/12/13/2286367.html static表示“全局”或者“静态”的意思,用来修饰成员变 ...
- Problem B: Excuses, Excuses!
Description Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame ex ...
- UVa 10806 Dijkstra,Dijkstra(最小费用最大流)
裸的费用流.往返就相当于从起点走两条路到终点. 按题意建图,将距离设为费用,流量设为1.然后增加2个点,一个连向节点1,流量=2,费用=0;结点n连一条同样的弧,然后求解最小费用最大流.当且仅当最大流 ...
- <转>java编译问题:使用了未经检查或不安全的操作
使用了未经检查或不安全的操作 在本人用editplus写java文件时碰到的问题. 源代码 import java.util.*; class collection{ public stat ...
- hadoop 主节点存储告警
之前只他调整过dfs 的存储目录到最大配额的目录,其它没有处理(就是在默认的/ 目录下,而这个目录的存储配额只有50G) 运行一周的时间不到,集群开始告警,查看是目录/ 的存储占用超过了60% 再查看 ...
- 关于asp.net 的一些好资料地址 , 防止丢失!
学习数据结构的好网站 : http://student.zjzk.cn/course_ware/data_structure/web/practice/practice1.htm http://www ...