THE PUZZLET PAGE

PUZZLET 009 - THE SOLUTION

This week's Puzzlet is an unusual combination of factorials and palindromes.  It's spiced up even further by the fact that 3 integers have to be found altogether and concatenated in ascending order to find the solution!  The problem is stated precisely in the REMs of the Ibasic program below, followed by code which finds the solution.

' Puzzlet #009.
' A certain integer possess the unusual property
' that the sum of the factorials of its digits
' is equal to the product of its digits. 2 other
' integers with the same property exist. The sum
' of the three integers is 1110. A palindrome is
' formed when they are concatenated in ascending
' order.  What are the integers?
' By Dave Ellis.


declare fact(n: int )
def count, i, j, k, prodDigs, sum, sumFacts: int
def integers[4]: int
openconsole

count = 0
for i = 1 to 6
    for j = 1 to 6
        for k = 1 to 6
            sumFacts = fact(i) + fact(j) + fact(k)
            prodDigs = i * j * k
            if prodDigs = sumFacts
                count = count + 1
                integers[count] = 100*i + 10*j + k
            endif
        next
k
    next j
next i

sum = 0
for i = 1 to 3
    print integers[i],
    sum = sum + integers[i]
next i
print: print "Sum: ", sum
print: print "That's all, folks!"
print: print "Press any key to continue ..."
do: until inkey$ <> ""
closeconsole
end

sub
fact(n)
' Returns the factorial of n (symbol n!)
def a, prod: int
prod = 1
for a = 1 to n
    prod = prod * a
next a
return prod
 
   
PROGRAM NOTES

Since the sum of all 3 integers is 1110, no individual integer can be greater than 1008.   Now, each integer must be the sum of the factorials of its own digits.  That is, if the integer is abc, we can say that abc = a! + b! + c!  It follows that the factorial of each digit must be less than 1006.  6! is 720 (less than 1006), while 7! is 5040 (greater than 1006).  Therefore, each digit must be 6 or less.

This establishes the search range for each digit is from 1 to 6.  The variables i, j, and k are used as iterators to check each digit through the whole of this range.

At each step, i, j, and k create a new integer to check.  The sum of the factorials of this integer's digits is calculated and stored in variable sumFacts.  The product of the digits is calculated and stored in variable prodDigs.

If sumFacts and prodDigs are equal, the current integer will be stored in the array integers[].  Before storing the integer's format must be changed, because only its digits are known, stored in i, j, and k.  To turn this into a normal denary number, we need to take the sum i*102 + j101 + k*100.  

The storage cell in integers[] is pointed to by variable count , which is always incremented to point to the next vacant position.

In a sense, the program is somewhat tongue in cheek - it assumes there is only one answer, and that the first three integers where sumFacts equals prodDigs will be the only ones.  When run, this proved to be the case.  If it hadn't the program could easily be modified to check the contents of integers[] each time variable count reached three, printing correct answers and rejecting invalid ones, resetting count to zero each time.

When you run the code, it produces the inset screen.

The final program notes below demonstrate that these three integers are indeed the correct solution.

334 343 433
Sum: 1110

That's all, folks!

Press any key to continue ...


          
The specification requires that the sum of the factorials of each integer's digits equals the product of those digits.   Let's look at the first integer, 334.

3! + 3! + 4! = 6 + 6 + 24 = 36.    3 x 3 x 4 = 36.    So the requirement is met for 334.  

Now, the other two integers, 343 and 433, are just numerical anagrams of the first, so they also meet the requirement (because the order of the digits doesn't matter for addition and multiplication).  Therefore the requirement is met for all three integers.

Finally, concatenating the integers in ascending order produces 334343433, a numerical palindrome, so this part of the specification is also met.  Voile!



MAIN MENU
DOWNLOAD CODE
ARCHIVES

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.