是的,有。核心思想:(1)它与 5 的最高次方能整除 n! 相同; (2) 这是 5 到 n 的倍数的个数,加上 25 到 n 的倍数的个数,再加上 125 到 n 的倍数的个数,等等。
但这不属于 Stack Overflow。
Yes there is. Key ideas: (1) it's the same as the highest power of 5 that divides n!; (2) that's the number of multiples of 5 up to n, plus the number of multiples of 25 up to n, plus the number of multiples of 125 up to n, etc.
1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x ... x (n-1) x n
?
The number of zeros in the decimal representation of n! is the number of times ten appears as a factor of that large number. Hence, the number of times 2x5 appears. Hence, as there will be many more occurrences of 2 as a factor than of 5 (why?), it is the number of times 5 is a factor of n!.
So, your interview question is: how many fives appear as factors of items in the expression
1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x ... x (n-1) x n
发布评论
评论(3)
是的,有。核心思想:(1)它与 5 的最高次方能整除 n! 相同; (2) 这是 5 到 n 的倍数的个数,加上 25 到 n 的倍数的个数,再加上 125 到 n 的倍数的个数,等等。
但这不属于 Stack Overflow。
Yes there is. Key ideas: (1) it's the same as the highest power of 5 that divides n!; (2) that's the number of multiples of 5 up to n, plus the number of multiples of 25 up to n, plus the number of multiples of 125 up to n, etc.
But this doesn't belong on Stack Overflow.
N 末尾的零的个数! 给出
由Σ Floor( n/5i )
,其中 i = 1,2,3.... C 中的简单代码
The number of zeros at the end of N! is given by
∑ floor( n/5i ) for i = 1,2,3....
Simple code in C
n! 的十进制表示形式中零的个数是十作为该大数的因子出现的次数。因此,2x5 出现的次数。因此,由于 2 作为因子的出现次数比 5 的出现次数要多(为什么?),因此它是 5 是 n! 的因子的次数。
所以,你的面试问题是:有多少个五作为项目的因素出现在表达式中
?
The number of zeros in the decimal representation of n! is the number of times ten appears as a factor of that large number. Hence, the number of times 2x5 appears. Hence, as there will be many more occurrences of 2 as a factor than of 5 (why?), it is the number of times 5 is a factor of n!.
So, your interview question is: how many fives appear as factors of items in the expression
?