In this problem we must be mindful of the possibility of overflow. As shown in the solutions yesterday, factorials get very big very quickly! The normal integer data type is 16 bits, and can store numbers up to 65535. However 9! is already as big as 362880.
So the trick is to only keep the last few non-zero digits of the factorial. But how many to keep? If we only keep one, the method falls over for 15!
14! = 87178291200
15! = 1307674368000
Now if we only kept the last non-zero digit of 14! (which is a 2), and multiplied it by 15, to get 30, we would conclude incorrectly that the last non-zero digit of 15! was a 3, and this is wrong. To fix it keep the last two non-zero digits of 14!, which are 12, which when multiplied by 15 gives 180, whose last non- zero digit is 8. Correct!
However we soon run into the same problem again.
24! = 620448401733239439360000
25! = 15511210043330985984000000
Now 36 times 25 is 900. Wrong again! Essentially the problem has occurred because multiplying by 25 has added two extra zeros to the factorial. When these get chopped off, we lose our accuracy. However this is the only case for which this method fails for numbers less than 100! The next failure occurs for 125!
(Yes, our solution provided yesterday was incorrect in this regard, and thanks to the student that pointed it out. However 25 was not in the test data)
For correct results for all numbers up to 100, either treat 25! as a special case, or keep the last three non-zero digits (936 times 25 = 23400). However this can lead to yet another problem! The numbers start to get too big, overflowing the integer data type. To deal with this, use the 32-bit long integer data-type, available in both QuickBasic and C. The three digit method itself also eventually breaks down for 375! (Do you begin to see a pattern in all this....)
Who said it was going to be easy?