C言語でやってみた。やっつけのひどいコードだけども。
#include <stdio.h> #include <stdlib.h> #include <string.h> /* WWWDOT - GOOGLE = DOTCOM: EとMは互換 */ /* W > G */ /* WDOTGLECM */ /* 012345678 */ typedef enum { false, true } bool; enum { W, D, O, T, G, L, E, C, M }; long word(a1, a2, a3, a4, a5, a6) { return a1 * 100000L + a2 * 10000L + a3 * 1000L + a4 * 100L + a5 * 10L + a6; } bool calculate(int a[]) { long WWWDOT, GOOGLE, DOTCOM; if (a[W] <= a[G]) { return false; } WWWDOT = word(a[W], a[W], a[W], a[D], a[O], a[T]); GOOGLE = word(a[G], a[O], a[O], a[G], a[L], a[E]); DOTCOM = word(a[D], a[O], a[T], a[C], a[O], a[M]); if ((WWWDOT - GOOGLE) == DOTCOM) { printf("%ld - %ld = %ld\n", WWWDOT, GOOGLE, DOTCOM); return true; } return false; } bool candidate(int c[]) { int b[] = {0,1,2,3,4,5,6,7,8,9, 0}; static int index = 9; if (index < 0) { return false; } if (1 <= index) { memcpy(c, b, index * sizeof(b[0])); } if (index <= 8) { memcpy(&c[index], &b[index + 1], (9 - index) * sizeof(b[0])); } index --; return true; } void concatinate(int a[], int c[], int index) { static bool used[9] = {0}; int i; for (i = 0; i < 9; i ++) { if (false == used[i]) { a[index] = c[i]; if (8 == index) { calculate(a); return; } else { used[i] = true; concatinate(a, c, index + 1); used[i] = false; } } } } int main() { int a[9], c[9]; while (candidate(c)) { /* 9個の数字を取り出す */ concatinate(a, c, 0); /* 並び替えて検証する */ } return EXIT_SUCCESS; }