Sunday 12 April 2009

PROJECT EULER #33

Link to Project Euler problem 33

The fraction ^(49)/_(98) is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that ^(49)/_(98) = ^(4)/_(8), which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, ^(30)/_(50) = ^(3)/_(5), to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.


using System;

namespace project_euler
{
class Program
{
static void Main()
{
//Problem 33
DateTime start = DateTime.Now;
double numerator = 1, denominator = 1;
double temp1=0,temp2=0;
for (double i = 10; i < 100; i++)
{
string a = i.ToString();
for (double j = i+1; j < 100; j++)
{
string b = j.ToString();

if (a[0] == b[1] && b[0] != '0'&& a[1]!='0')
{
temp1 = double.Parse(a[1].ToString())/double.Parse(b[0].ToString());
if (temp1==i/j)
{
numerator *= i;
denominator *= j;
}
}
else if (a[1] == b[0] && b[1] != '0' && a[0]!='0')
{
temp2 = double.Parse(a[0].ToString())/double.Parse(b[1].ToString());
if (temp2==i/j)
{
numerator *= i;
denominator *= j;
}
}
}
}
bool test = true;
while(test)
{
for (int i = 1; i <= numerator; i++)
if (numerator%i == 0 && denominator%i == 0)
{
numerator /= i;
denominator /= i;
}
if (denominator%numerator != 0 || numerator == 1)
test = false;
}
Console.WriteLine(denominator);
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
}
}

No comments: