Problem 4:
Sum of modular factorials
The factorial of a number, n!, is defined as the product of all
numbers from 1 to n. For example, 3! = 3 * 2 * 1 = 6. The sum
of all factorials is clearly infinite. A modular factorial is
a factorial mod some number m. Surprisingly, for a given m, the sum
of all modular factorials is finite and can be computed. This is
clearly the case because for any given m, and any number k >= m,
k! % m = 0.
Write a program that will, given an integer m input from the keyboard,
compute and print out the sum of all modular factorials of m.
For example, the sum of all factorials mod 4 is:
(1!)%4 + (2!)%4 + (3!)%4 + (4!)%4 ... = 1 + 2 + 2 + 0 ... = 5.
Thus, given the input 4, your program should print out 5.
To hand in the program, use the command:
handin acmjudge prog4