(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 ...
随机推荐
- 什么是epoll
什么是epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.当然,这不是2.6内核才有的,它是在2.5.44内核中被引进的(epoll(4) is a new A ...
- CCNP路由实验(2) -- OSPF
OSPF作为一种内部网关协议(IGP),用于在同一个AS中的路由器之间交换路由信息.OSPF的特性如下:1.可适应大规模网络2.收敛速度快3.无路由环路4.支持VLSM和CIDR5.支持等价路由6.支 ...
- windows平台发消息到非UI线程.
下面的代码是介绍如何在windows平台发消息到非UI线程. 主要是'PeekMessage || GetMessage' 这两个API的应用. 当他们被调用的时候,如果当前线程还没有消息循环,就会创 ...
- makefile简单helloworld
最近要在unix系统上开发c++应用程序,但默认情况下unix编译c++程序需要使用makefile.其实makefile语法还是比较简单,看上去有点像ant.废话不说了,直接上helloworld. ...
- BootStrap 智能表单系列 首页 (持续更新中...)
背景:本码农.NET后端工程师,在项目开发中发现写了很多重复的代码, 于是自己整了一套根据配置来生成form表单的插件,针对表单的改动仅需要修改配置的json即可 使用中发现还是蛮实用的,于是开源出来 ...
- 文件上传下载样式 --- bootstrap
在平时工作中,文件上传下载功能属于不可或缺的一部分.bootstrap前端样式框架也使用的比较多,现在根据bootstrap强大的样式模板,自定义一种文件下载的样式. 后续会使用spring MVC框 ...
- Foundation 框架 NSFileManager,NSData 简单的文件操作
一.简单展示NSFileManager的使用 #import <Foundation/Foundation.h> int main(int argc, const char * argv[ ...
- 手机测试Android程序
手机测试Android程序 上传者:sanpi329 我也要“分享赚钱” 2014/7/9 关注(23) 评论(0) 声明:此内容仅代表网友个人经验或观点,不代表本网站立场和观点. ...
- Eclipse+ADT的环境搭建
Index: . Java环境变量的设置 . Android环境变量的设置 1.Java环境变量的设置 A.属性名称:JAVA_HOME 属性值:C:\Program Files\Java\jdk1. ...
- IC卡,ID卡,M1卡,射频卡
一般把可读可写,频率是13.56MHz的射频卡称为IC卡,IC卡可以写入数据, 只能读,频率是125KHz的射频卡称为ID卡, M1卡是NXP公司的S50卡的一种叫法,国内的复旦F08,达华的TKS5 ...