题目描述:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
给定整数n,找出小于n的数中,找出阶乘末尾为0的数的个数,
LeetCode Factorial Trailing Zeroes
。本题如果分别求1!,2!...n!,根本无法通过测试数据。
规律为:对于数字m!∈(0,n] ,如果m!末尾为0,那么必有1个因数为5和2,
电脑资料
《LeetCode Factorial Trailing Zeroes》(https://www.unjs.com)。因此题目便转化为:统计(0,n]之间,5约数个数和。
实现代码:
public class Solution { public int TrailingZeroes(int n) { var c = 0; var factor = 5; var end = int.MaxValue / 5; while(n >= factor && factor != int.MaxValue){ c += n/factor; if(factor > end){ factor = int.MaxValue; } else{ factor *= 5; } } return c; }}