We use the fact that zeros occurs only when multiple of 5 gets multiplied to an even number. So it boils down to a problem of finding power of 5 in prime factorization of n!.
int countOfPrimeFactors(int n,int k) // This method gives count of prime factors of k for a given n in n! .
{
int count=0;
while(n>=k)
{
count+=n/k;
k= k*k;
}
return count;
}
main()
{
int n = 20;
printf("Number of zeros : %d\n", countOfPrimeFactors(n,5)); // Find prime factors of 5 in n! .
}
No comments:
Post a Comment