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 way 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
using namespace std;
 * 1) include

int main {  long long int number,temp,occur=0; while(cin>>number && number!=0) {       int i=0; temp=number; cout<<number<<"! = "; int b [] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; while(b[i]<=temp) {          while(temp!=0) {             temp/=b[i]; ++occur; }          cout<<occur<<" "; occur=0; }        cout<<endl; }  return 0; }