//------------------------------------------------------------------- // enigma1358.c (c) 2005 by Charles Petzold (www.charlespetzold.com) // // "Five Fives" by Adrian Somerfield, // New Scientist, 17 September 2005, page 50. // // Five numbers in five different number bases. All five numbers are // 11101, and the first four total to the fifth. What's the total? // // We are also told that all five numbers are even. That means that // all the number bases are odd. (Any number in an even number base // ending in 0 is even. Add 1 and it's odd.) // // We are also told that the total is less than 100,000. That restricts // the bases to 17 or lower. // // We're really only dealing with 8 possible numbers, as shown in the // following chart: // // Base 11101 Converted to Decimal // ---- -------------------------- // 3 118 // 5 776 // 7 2,794 // 9 7,372 // 11 16,094 // 13 30,928 // 15 54,226 // 17 88,724 // // With these possibilities, it's not too hard to determine that the // solution is 776 + 2,794 + 30,928 + 54,226 = 88,724. // // Let's code it up anyway. //---------------------------------------------------------------------- #include // The function assumes the first argument is a number in a number // base given as the second argument. The number is converted to decimal. // // This function obviously cannot work for number bases greater than 10 // if the number has digits greater than 9, but that's not a problem // here. int ConvertToDecimal(int num, int base) { int result = 0; int multiplier = 1; while (num > 0) { result += multiplier * (num % 10); num /= 10; multiplier *= base; } return result; } int main(void) { int base1, base2, base3, base4, base5; int num1, num2, num3, num4, num5; // Five loops for the five numbers. Let's arrange them so // the first number is the lowest. The number base can be // 3, 5, 7, or 9. for (base1 = 3; base1 < 11; base1 += 2) { num1 = ConvertToDecimal(11101, base1); // The second number base is greater than the // first but can go up to 11. for (base2 = base1 + 2; base2 < 13; base2 += 2) { num2 = ConvertToDecimal(11101, base2); // And so forth. for (base3 = base2 + 2; base3 < 15; base3 += 2) { num3 = ConvertToDecimal(11101, base3); for (base4 = base3 + 2; base4 < 17; base4 += 2) { num4 = ConvertToDecimal(11101, base4); for (base5 = base4 + 2; base5 < 19; base5 += 2) { num5 = ConvertToDecimal(11101, base5); // If the 5th number is the sum of the first four, // the problem is solved. if (num5 == num1 + num2 + num3 + num4) { printf("%i\n", num5); } } } } } } return 0; }