Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

 

  1. int trailingZeroes(int n) {
  2. int base = 5;
  3. int cnt = 0;
  4. while(n>=base) {
  5. cnt += n/base;
  6. base *= 5;
  7. }
  8. return cnt;
  9. }
posted @ 2014-12-30 20:02  purejade  阅读(64)  评论(0)    收藏  举报