//--------------------------------------------------------------------- // enigma1378.c (c) 2006 by Charles Petzold // // "Exaudi Deus" by Adrian Somerfield, // New Scientist, 11 February 2006, page 54 // // Five ascending numbers, all perfect squares less than 1000. // Each digit that appears occurs exactly three times. // What's the second number? //--------------------------------------------------------------------- #include int main(void) { int i, n[5]; // Loop through all combinations of 5 ascending numbers // whose squares are less than 1000 for (n[0] = 1; n[0] < 28; n[0]++) for (n[1] = n[0] + 1; n[1] < 29; n[1]++) for (n[2] = n[1] + 1; n[2] < 30; n[2]++) for (n[3] = n[2] + 1; n[3] < 31; n[3]++) for (n[4] = n[3] + 1; n[4] < 32; n[4]++) { int count[10]; // Zero out the count array for (i = 0; i < 10; i++) count[i] = 0; // Go through 5 numbers for (i = 0; i < 5; i++) { // Calculate the square int num = n[i] * n[i]; // Accumulate the digit counts while (num) { count[num % 10]++; num /= 10; } } // Check if each digit appears 0 or 3 times for (i = 0; i < 10; i++) if (count[i] != 0 && count[i] != 3) break; // Success if i equals 10 if (i == 10) { for (i = 0; i < 5; i++) printf("%i ", n[i] * n[i]); printf("\n"); } } return 0; }