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.

Just a bunch of cascaded if’s.

public class Euler33
{
    public static void main(String[] args)
    {
        System.out.print(“Problem 33:\n”);
        Euler33 e = new Euler33();
        System.out.print(“Answer = ” + e.Problem()+ “\n”);
    }
    public String Problem ()
    {
        int numProd = 1, denomProd = 1;
        String [] digits = {“1″,”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9”};
        for (int num = 11; num <= 99; num++)
            for (int denom = 11; denom <= 99; denom++)
                if ((double)num/(double)denom < 1)
                {
                    String numStr = String.valueOf(num);
                    String denomStr = String.valueOf(denom);
                    for (String d : digits)
                        if (numStr.contains(d) && denomStr.contains(d))
                        {
                            String newNum = numStr.replace(d,””);
                            String newDenom = denomStr.replace(d,””);
                            if (newNum.equals(“”) == false && newDenom.equals(“”) == false)
                                if (newDenom.equals(“0”) == false)
                                    if ((double)num/(double)denom == (Double.valueOf(newNum)/Double.valueOf(newDenom)))
                                    {
                                        System.out.format (“%d / %d = %s / %s\n”, num, denom, newNum, newDenom);
                                        numProd *= Double.valueOf(newNum);
                                        denomProd *= Double.valueOf(newDenom);
                                    }
                        }
                }
        return String.valueOf(denomProd/numProd);
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s