It is possible to show that the square root of two can be expressed as an infinite continued fraction.
Sqrt(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?
using System;
namespace project_euler
{
class Program
{
static void Main()
{
//Problem 57
//(a+b)/b, where b = previous a+2*b and a = previous a + b.
DateTime start = DateTime.Now;
int count=0;
BigInt a = new BigInt(3);
BigInt b = new BigInt(2);
for (int i = 1; i < 1000; i++)
{
if (a.ToString().Length > b.ToString().Length)
count++;
a = 2*b + a;
b = a - b;
}
Console.WriteLine(count);
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
}
}
No comments:
Post a Comment