UVa 160 - Factors and Factorials

160 - Factors and Factorials

 * http://acm.uva.es/p/v1/160.html

Summary
Bruteforce on all primes up to 100.

Explanation
Since the upper limit for N is 100, then the largest prime that can divide N! is 97. This is a rather small number, so we can simply walk the list of primes in any old fashion and see how many times it divides into N!. Since N is so small, it is very hard to have a solution time out. There is an another solution for finding this. Let we want to know how many times is 2 occurred in 100! ? For finding this proceed by the following manner : 100/2 = 50    50/2=25     25/2=12      12/2=6     6/2=3     3/2=1 --By summing the quoetient we get (50+25+12+6+3+1)=97 .Can you tell me how many times is 3 occurred in 2000! ? All the best wishes.

Gotcha's
N! for N = 100 will not fit into any integral type, you must determine divisibility in another manner.

Input
2 5 53 100 0

Output
2! = 1  5! =  3  1  1 53! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1        1 100! = 97 48 24 16  9  7  5  5  4  3  3  2  2  2  2        1  1  1  1  1  1  1  1  1  1

Solution

 * C++: http://acm-solution.blogspot.com/2010/11/acm-uva-160-factors-and-factorials.html