Project Euler:Problem 57 Square root convergents

原创 2015年07月07日 22:53:39

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?


找规律,大数加法


#include <iostream>
#include <string>
using namespace std;

string strplus(string a, string b)
{
	int lena = a.length();
	int lenb = b.length();
	int len;
	if (lena <= lenb)
	{
		int gap = lenb - lena;
		string c;
		c.resize(gap, '0');
		a = c + a;
		len = lenb;
	}
	else
	{
		int gap = lena - lenb;
		string c;
		c.resize(gap, '0');
		b = c + b;
		len = lena;
	}
	int flag = 0;
	string ans = "";
	for (int i = len - 1; i >= 0; i--)
	{
		int tmp = a[i] + b[i] - '0' - '0' + flag;
		flag = tmp / 10;
		tmp = tmp % 10;
		char p = tmp + '0';
		ans = p + ans;
	}
	if (flag == 1)
		ans = '1' + ans;
	return ans;
}

int main()
{
	string a = "3";
	string b = "2";
	int count = 0;
	for (int i = 2; i <= 1000; i++)
	{
		string tmp = b;
		b = strplus(a, b);
		a = strplus(b, tmp);
		if (a.length() > b.length())
			count++;
	}
	cout << count << endl;
	system("pause");
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Project Euler:Problem 65 Convergents of e

The square root of 2 can be written as an infinite continued fraction. √2 = 1 + 1 ...
  • youb11
  • youb11
  • 2015年07月15日 14:57
  • 610

project euler problem 5

  • 2010年12月18日 10:58
  • 18KB
  • 下载

Project Euler:Problem 49 Prime permutations

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...
  • youb11
  • youb11
  • 2015年06月07日 11:12
  • 394

Project Euler:Problem 22 Names scores

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-t...
  • youb11
  • youb11
  • 2015年06月01日 21:01
  • 529

Project Euler Problem 32 Pandigital products

Pandigital products Problem 32 We shall say that an n-digit number is pandigital if it makes use ...

Project Euler:Problem 51 Prime digit replacements

By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible valu...
  • youb11
  • youb11
  • 2015年07月01日 19:58
  • 509

Project Euler:Problem 86 Cuboid route

A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o...
  • youb11
  • youb11
  • 2015年07月22日 17:04
  • 580

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma...
  • youb11
  • youb11
  • 2015年05月31日 10:54
  • 687

Project Euler - Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any rema...

Project Euler:Problem 50 Consecutive prime sum

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 ...
  • youb11
  • youb11
  • 2015年06月07日 14:20
  • 393
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Project Euler:Problem 57 Square root convergents
举报原因:
原因补充:

(最多只允许输入30个字)