2.2 The beauty of programming-Do not be scared by the factorial to [zero count of N factorial], the beauty of Programming
2.2 The beauty of programming-Do not be scared by the factorial [zero count of N factorial]
[Link to this article]
Http://www.cnblogs.com/hellogiser/p/zero-count-of-N-factorial.html
[Question]
Question 1:Given an integer N, the factorial N of N! How many zeros are there at the end? Example: N = 10, N! = 3 628 800, N! There are two zeros at the end.
Idea: This is mainly to judge the number of 5 in each number, because after multiplication of 5 and an even number, we can get 10, which is equivalent to adding a 0.
Question 2: N! In binary format.
Idea: At first glance, it seems that question 2 has nothing to do with Question 1. However, from another perspective, if the binary system returns 0 after the bitwise 1, then the position of the bitwise 1 is the number of zeros after the bitwise 1, it is the same as problem 1, except that one is a decimal table and the other is a binary representation. Here, in all the numbers less than N, the multiples of 2 contribute a multiples of 0, 4 and then 0, and so on. Because the binary representation is actually based on 2, each occurrence of a 2 will end with a 0, so you only need to find N! The number of factor 2.
[Code]
C ++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: 2014/7/8 */
//----------------------------------------------- // 1. calculate N! How many zeros are there at the end? //-----------------------------------------------
/* Solution 1: Calculate the 5 index of I (I = 1, 2, 3. N) factorization */ Int count (int n) { Int ret = 0; Int I, j;
For (I = 1; I <= N; I ++) { J = I; While (0 = j % 5) { Ret ++; J/= 5; } }
Return ret; }
/* Solution 1: optimize the cycle and set the cycle step to 5 */ Int count (int n) { Int ret = 0; Int I, j;
// Set the cyclic step to 5. For (I = 5; I <= N; I = I + 5) { J = I; While (0 = j % 5) { Ret ++; J/= 5; } }
Return ret; }
/* Solution 2 z = [N/5] + [N/(5*5)] + [N/(5*5*5)] ...... */ /* [N/5] indicates the number of 5 in N, and [N/(5*5)] indicates the number of 5 in [N/5 */ /* Z is N! Contains the number of prime numbers 5 */ Int count (int n) { Int ret = 0;
While (n) { Ret + = N/5; N/= 5; }
Return ret; }
//----------------------------------------------- // 2 calculate N! Binary represents the position of the second digit 1. //-----------------------------------------------
/* 2 = (10), every occurrence of a 2, 1 forward 1 bits, such as binary 10*10*10*10 = (10000 )*/ /* Equal to N! Add 1 to the number containing prime factor 2 */ /* Z = [N/2] + [N/(2*2)] + [N/(2*2*2)] ...... */ Int lastone (int n) { Int ret = 0; While (n) { N> = 1; Ret + = n; }
Return ret; }
/* Determine whether n is the power of 2 */ Bool is2n (int n) { Return n> 0 & (0 = (n & (n-1 ))); } |
[Reference]
Http://blog.csdn.net/eric43/article/details/7570474
Http://blog.csdn.net/zcsylj/article/details/6393308
[Link to this article]
Http://www.cnblogs.com/hellogiser/p/zero-count-of-N-factorial.html