I'm trying to solve a cryptarithmetic puzzle TOO + TOO + TOO + TOO = GOOD using a program.

My solution to this, is to use a nested loop for each unique letter (in this case T, O, G, D). The loops would systematically assign the digits from 0-9 to each letter. For example, it might first try T = 0, O = 0, G = 0, D = 0, then T = 0, O = 0, G =0, D = 1, then T = 0, O = 0, G = 0, D = 2, etc. up to T = 9, O = 9, G = 9, D = 9.

Would this be the correct way to do this? If it is, the answer I got using my program was 1642 which I know isn't correct considering there's only 3 values instead of 4 (TOO)

Recommended Answers

All 8 Replies

Thanks for the reply but I need make a program using the loop to give me that answer xD

Thanks for the reply but I need make a program using the loop to give me that answer xD

Well if you wanted to, you could just brute-force it. There are only three letters and hence 10 * 9 * 8 possibilities:

for (int g = 0; g < 10; g++)
{
    for (int o = 0; o < 10; o++)
    {
        if (g != o)
        {
             for (int d = 0; d < 10; d++)
             {
                 if (g != d && o != d)
                 {
                      if (SentenceWorks (g, o, d))
                          // display g, o, and d
                 }
             }
        }
    }
}

SentenceWorks is a boolean function that replaces the letters with values, evaluates the terms, and sees if they are the same.

[EDIT]
Whoops. there are four letters, not three, so add a t loop. Same concept though. It's the "dumb", brute force way, bu it'll get the job done. There are probably more elegant solutions.
[/EDIT]

Unfortunetly that code doesnt work for me. I don't get the correct answers with that x_x

Unfortunetly that code doesnt work for me. I don't get the correct answers with that x_x

It will work. Post your code.

this code will solve only this problem and will do it in 36 iterations. just did it for giggles to see if i could. if you want a generic code then you will have to do other things ;)

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	string too = "", good = "";
	char convert[5];
	int temp;
	for (int i = 1; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == i)
				continue;
			temp = (i * 100) + (j * 10) + j;
			_itoa(temp, convert, 10);
			too = convert;
			_itoa((temp * 4), convert, 10);
			good = convert;
			if ((too[2] == good[1]) && (good[1] == good[2]) && (good[0] != good[3]) && (good[1] != good[3]) && (good.size() == 4))
			{
				cout << "SUCCESS!!!\n";
				cout << "TOO = " << temp << "\n";
				cout << "GOOD = " << (temp * 4) << "\n";
				cin.get();
				return 0;
			}
			cout << "too = " << temp << "\tgood = " << (temp * 4) << "\n";
		}
	}
	cout << "sorry this didnt work.";
	cin.get();
	return 0;
}

Thanks for the suggestion!

#include <iostream>
using namespace std;

int main()
{

    for( int t = 0; t < 10; t++ )
        for( int o = 0; o < 10; o++ )
        // put g starting at 1 so != to 0
            for( int g = 1; g < 10; g++ ) 
                for( int d = 0; d < 10; d++)
                //first if makes sure none of the letter are the same
                    if (t != o && t != d && t != g && g != o && g != d && d != o)
                    //second if checks if they are equal 
                        if ((t*100 + o*10 + o) * 4 == g*1000 + o*100 +o*10 +d){
                            cout << g << o << o << d << " = good\n";
                            cout << t << o << o << " = too";
                        } 
    return 0;
}
commented: This post is over 4 years old? :S! What is the point? -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.