Project Euler

Project Euler Problem 33

steloflute 2012. 6. 9. 00:03

Problem 33

20 December 2002

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.


Answer:
100

 

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.IO;

namespace Euler {
    class Program {
        static void Main(string[] args) {
            double product = 1.0;
            foreach (var a in Enumerable.Range(1, 9)) {
                foreach (var b in Enumerable.Range(1, 9)) {
                    foreach (var c in Enumerable.Range(1, 9)) {
                        int numerator = 10 * a + c;
                        int denominator = 10 * c + b;
                        if (numerator < denominator && numerator * b == denominator * a) {
                            product *= (double)a / b;
                            Console.WriteLine("{0}{2} / {2}{1}", a, b, c);
                        }
                        numerator = 10 * c + a;
                        denominator = 10 * b + c;
                        if (numerator < denominator && numerator * b == denominator * a) {
                            product *= (double)a / b;
                            Console.WriteLine("{2}{0} / {1}{2}", a, b, c);
                        }
                    }
                }
            }
            Console.WriteLine(product);
        }
    }
}



'Project Euler' 카테고리의 다른 글

Project Euler Problem 35  (0) 2012.06.09
Project Euler Problem 34  (0) 2012.06.09
Project Euler Problem 32  (0) 2012.06.09
Project Euler Problem 31  (0) 2012.06.09
Project Euler Problem 30  (0) 2012.06.09