/** * puzzle.c * A brute force approach to a simple puzzle. */ #include /** * Determine if X is a solution to the puzzle. */ int is_solution (int X) { int Y = 2*X; // Y from the puzzle unsigned int digits = 1; // A bit set to represent the digits seen unsigned int bit; // One bit in the set int i; // Generic counter variable // Process the digits in X for (i = 0; i < 4; i++) { bit = 0x1 << (X % 10); if (digits & bit) return 0; digits |= bit; X = X / 10; } // Process the digits in Y for (i = 0; i < 5; i++) { bit = 0x1 << (Y % 10); if (digits & bit) return 0; digits |= bit; Y = Y / 10; } // If we've made it this far, we've got a solution. return 1; } // is_solution int main () { int X; for (X = 0; X <= 9999; X++) if (is_solution (X)) printf ("X = %d, Y = %d\n", X, X*2); return 0; } // main