the beauty of programming has a topic about factorial:
1 Given an integer n, then the factorial of n equals n!, how many 0 at the end, for example, n=10,n!=3628800,n! at the end of two 0
2 The position of the lowest bit in the binary representation of the n! is 1.
factorial definition:
N!={1 n(n? 1 )! n=0 n>0 ?N∈N
The author's analysis of Problem one: the factorization of n!: n!=2x*3y*5z, because 10=2*5, so the number of M with 2 and 5 is X, Z. Each pair of 2 and 5 can get 10, so M=min (X,z). Because the number that can be divisible by 2 is higher than the frequency that can be divisible by 5, so m=z.
Solution One:
calculates the exponent of 5 in the factorization directly and sums it.
Code implementation:
#include <iostream>usingnamespacestd;int findZero(int N){ int ret=0; for(int i=1;i<=N;i++) { int j=i; //计算每个阶乘相乘数字分解因式5的个数 while(j%5==0) { ret++; j/=5; } } return ret;}
Solution Two:
according to [n/k] is equal to the number of "N-a", ..., can be divided by K numbers in the law, the following formula is obtained:
z=[n/5]+[n/ < Span class= "mn" id= "mathjax-span-55" style= "Font-family:mathjax_main;" >5 2 ]+[n/ < Span class= "mn" id= "mathjax-span-60" style= "Font-family:mathjax_main;" >5 3 ]+ .... (There is always a k that makes < Span class= "mn" id= "mathjax-span-65" style= "Font-family:mathjax_main;" >5 k Span style= "Display:inline-block; width:0px; Height:2.456em; " > >n,[n/ < Span class= "mn" id= "mathjax-span-70" style= "Font-family:mathjax_main;" >5 k Span style= "Display:inline-block; width:0px; Height:2.456em; " > ]=0)
In the formula, [N/5] represents a multiple of 5 not greater than N to contribute a 5,[n/ < Span class= "mn" id= "mathjax-span-75" style= "Font-family:mathjax_main;" >5 2 ] Represents a number that is not greater than n < Span class= "mn" id= "mathjax-span-80" style= "Font-family:mathjax_main;" >5 2 The multiples of the contribution again a 5 ....
Code implementation:
#include <iostream>usingnamespacestd;int findZero(int N){ int ret=0; while(N) { //计算1,2,3...k能被当前5的指数倍数整除个数 ret+=N/5; //累计5的指数 N/=5; } return ret;}
for question two, really bad think of that aspect, we all know in the computer binary system, there is a rule, that is if a number is even, then the number of the last binary must be 0, if it is odd, then the number of the last binary must be 1, here the author according to the law gave two methods.
Solution One:
determines whether the last bits is 0, or 0, moves the binary number right one bit, which is the quotient value, and conversely, if 1, the binary number is odd and cannot be divisible by 2.
So, this problem is actually equivalent to seeking n! Contains the number of factorization 2. That answer equals n! Contains the number of factorization 2 plus 1.
Code implementation:
#include <iostream>usingnamespacestd;int lowestOne(int N){ int ret=0; //统计被2整除的个数 while(N) { N>>=1; ret+=N; } return ++ret;}
Solution Two:
N! Contains the number of factorization 2, also equals n minus n in the binary representation of 1. We can also solve it by this law.
Under this rule for example, assuming N = 11011, then n! contains factorization 2 of the number is N/2 + N/4 + n/8 + n/16 + ...
i.e.: 1101 + + + 1
= (+ + + 1)
+ (+ +)
+ (+ 1)
+ 1
= (1000 + 100+ 10 + 1) + (100 + 10 + 1) + 1
= 1111 + 111 + 1
= (10000-1) + (1000-1) + (10-1) + (1-1)
= 11011-n binary representation 1 number
code implementation: p>
#include <iostream>usingnamespacestd;int lowestOne(int N){ int ret=0; int temp=N; //统计被2整除的个数 while(temp) { ret+=temp&1; temp>>=1; } ret=N-ret; return ++ret;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Don't be intimidated by factorial.