lundi 13 juin 2016

Cryptarithmetic puzzle


I am trying to solve this cryptarithmetic puzzle TWO + TWO = FOUR and I used a raw brute force, but I can't figure out where I am making mistake. It is quite simple, so I don't think that there is a need for explanation of the code.

int T, W, O, F, U, R;

for (T = 0; T < 10; T++)
{
    for (W = 0; W < 10; W++)
    {
        for (O = 0; O < 10; O++)
        {
            for (F = 0; F < 10; F++)
            {
                for (U = 0; U < 10; U++)
                {
                    for (R = 0; R < 10; R++)
                    {
                        if ((T == W) || (T == O) || (T == F) || (T == U) || (T == R) || (W == O) || (W == F) || (W == U) || (W == R) || (O == F) || (O == U) || (O == R) || (F == U) || (F == R) || (U == R))
                        {
                            continue;
                        }
                        else if (200 * T + 20 * W + 2 * O == F * 1000 + O * 100 + U * 10 + R * 0) {
                            cout << "T = " << T << endl
                                << "W = " << W << endl
                                << "O = " << O << endl
                                << "F = " << F << endl
                                << "U = " << U << endl
                                << "R = " << R << endl << endl;
                            break;
                        }
                    }
                }
            }
        }
    }
}

I get bunch of results and interestingly enough, only the last result is fine, where it gives:

T = 7
W = 6
O = 5
F = 1
U = 3
R = 0

Aucun commentaire:

Enregistrer un commentaire