Trailing Zeros

Write an algorithm which computes the number of trailing zeros in n factorial.

Have you met this question in a real interview?

Yes

Example

11! = 39916800, so the out should be 2

 1 class Solution {
 2     /*
 3      * param n: As desciption
 4      * return: An integer, denote the number of trailing zeros in n!
 5      我们会发现: the number of 2s in prime factors is always more than or equal
 6      to the number of 5s. So if we count 5s in prime factors, we are done.
 7
 8     How to count total number of 5s in prime factors of n!? A simple way is
 9     to calculate floor(n/5).
10
11     问题转化为求阶乘过程中质因子5的个数,但是要注意25能提供2个5,125能提供3个5....
12     所以,count= floor(n/5) + floor(n/25) + floor(n/125) + ....
13      */
14 public long trailingZeros(long n) {
15         if (n < 0) return 0;
16
17         long total = 0;
18         long base = 5;
19         while (base <= n) {
20             total += n / base;
21             base *= 5;
22         }
23         return total;
24     }
25 };
时间: 2024-10-16 23:08:01

Trailing Zeros的相关文章

[LintCode] Trailing Zeros

Write an algorithm which computes the number of trailing zeros in n factorial. Example 11! = 39916800, so the out should be 2 Challenge O(log N) time 1 class Solution { 2 public long trailingZeros(long n) { 3 long cnt = 0; 4 while(n != 0){ 5 cnt += n

[LeetCode] Factorial Trailing Zeros

Well, to compute the number of trailing zeros, we need to first think clear about what will generate a trailing 0? Obviously, a number multiplied by 10 will have a trailing 0 added to it. So we only need to find out how many 10's will appear in the e

[Lintcode]2. Trailing Zeros

2. Trailing Zeros 本题难度: Easy Topic: Math&Bit Manipulation Description Write an algorithm which computes the number of trailing zeros in n factorial. Example Example 1: Input: 11 Output: 2 Explanation: 11! = 39916800, so the output should be 2 Example

[LeetCode]65. Factorial Trailing Zeros阶乘的尾零个数

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. Subscribe to see which companies asked th

Jan 07 - Factorial trailing zeros; DAC; Number;

public class Solution { public int trailingZeroes(int n) { int result = 0; while(n > 0){ n = n/5; result += n; } return result; } } 需要注意 个别数 5^n;

[LintCode] Trailing Zeroes 末尾零的个数

Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this question in a real interview? Yes Example 11! = 39916800, so the out should be 2 Challenge O(log N) time s

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

Count the consecutive zero bits (trailing) on the right with multiply and lookup

我在网上看到了一点神奇的代码,用来计算一个数字末尾连续零的个数. 刚好我在优化一个I2C读写函数(只写入I2C特定bit),觉得这个很有用.经过尝试,确实没问题. 下面我隆重介绍一下: Count the consecutive zero bits (trailing) on the right with multiply and lookup unsigned int v; // find the number of trailing zeros in 32-bit v int r; // r

code force 2B The least round way

There is a square matrix n?×?n, consisting of non-negative integer numbers. You should find such a way on it that starts in the upper left cell of the matrix; each following cell is to the right or down from the current cell; the way ends in the bott