Search This Blog

Tuesday, December 28, 2010

Euler Problem 33

I found this puzzle quite tricky to understand… perhaps the question could be written more clearly…

Essentially you just need to find all the fractions which have no more than 2 digits in top/bottom (ie <99/99), which are equal to <1, and which have the same number in the top and bottom which when that number is removed from the fraction the result is the same as the full fraction… anything using factors of 10 is ignored.

So 10/20 = 1/2 (remove 0 from top and base) – but this simple example is ignored.. they only want more complex ones like…

16/64 = 0.25 … remove those common 6s and you’d have 1/4 which is also 0.25 so this what they’re after…! In fact you now only need find the other 3!

I did it by using MOD10 to get the last number, and (Convert.ToInt16(t/10)) to get the first… then just compare these to find when numbers match… could be done using string conversions otherwise..

_______________________

static void Main(string[] args)
{
double sum = 1;

for (int b = 1; b < 100; b++) //t/b has to be <1
{
for (int t = 1; t < b; t++) //only check up to 99/98
{
double f= Convert.ToDouble(t) / Convert.ToSingle(b);
double i= Convert.ToDouble(Convert.ToInt16(t/10));
double j= Convert.ToDouble(Convert.ToInt16(b/10));
double x = 0;

//skip easy ones that divide by 10 top and bottom
if ( (t%10!=0 && b%10!=0))
{
//(i == j || i == b % 10 || t % 10 == j || t % 10 == b % 10) )

if (i == j) { x = (t % 10) / (b % 10); }
if (i == b%10) { x = (t % 10) / j; }
if (t%10 == j) { x = i / (b % 10); }
if (t%10 == b%10) { x = i / j; }

if (x == f)
{
sum *= f;
Console.WriteLine(t + "/" + b);
}

}
}
}

Console.WriteLine("Answer>>" + 1/sum);
Console.ReadLine();

}

No comments: