Factorial Frequencies
Time limit1sMemory limit128 MB
Read n values until a terminating 0, and for each n count how many times each digit 0 through 9 appears in the decimal representation of n! (n up to 366), printing the counts in a fixed two-line format.
- Level
Easy3 of 10
- Topics
- Math, Implementation, Simulation
- Solved
- No attempts yet
Problem
To boost her struggling palm-reading business, Madam Phoenix has decided to offer her customers a few numerology novelties. She has convinced them that the frequency with which each digit appears in the decimal representation of a factorial reveals something about their future. Unlike palm-reading, though, she cannot simply invent these frequencies, so she has hired you to compute them.
Recall that (that is, factorial) is defined as . Because she plans to use the day of the week, the day of the month, or the day of the year as the value of , your program must be able to count how many times each decimal digit occurs in numbers as large as , which has digits.
Input
The input is a list of integers, one per line, for which the digit counts are wanted. Every input value is greater than and less than or equal to , except for the last value, which is . Do not process this final ; stop reading at that point.
Output
For each input value (processed in order), print a line of the form n! --. Then print the count of each decimal digit in across two lines: digits through on the first line and digits through on the second.
Each digit entry is printed as three spaces, the digit in parentheses, the count right-justified in a field five characters wide, and a single trailing space — that is, the C-style format (%d)%5d repeated for the five digits on the line. Both lines therefore end with a trailing space. Follow this output format exactly.
Hint
Madam Phoenix will be forever (or longer) in your debt; she might even send you on a trip if you do the job well!