R: number of trailing zeros of factorial
I think you should apply some mathematical properties of factorial
before counting the number of trailing zeros, see https://mathworld.wolfram.com/Factorial.html
In factorial(n)
, you should know that the number of trailing zeros depends on the pairs of 2*5
in the cascaded product 1*2*...*n
. In this case, you can define your custom function zeros
like below
zeros <- Vectorize(function(n) ifelse(n>=5,sum(sapply(seq_along(floor(logb(n,5))), function(p) floor(n/5**p) )),0))
then you can add the column of trailing zeros via
df <- within(df,trail0s <- zeros(n))
such that
> df
n trail0s
1 1 0
2 2 0
3 3 0
4 4 0
5 5 1
6 6 1
7 7 1
8 8 1
9 9 1
10 10 2
11 11 2
12 12 2
13 13 2
14 14 2
15 15 3
16 16 3
17 17 3
18 18 3
19 19 3
20 20 4
21 21 4
22 22 4
23 23 4
24 24 4
25 25 5
26 26 5
27 27 5
28 28 5
29 29 5
30 30 6
31 31 6
32 32 6
33 33 6
34 34 6
35 35 7
36 36 7
37 37 7
38 38 7
39 39 7
40 40 8
41 41 8
42 42 8
43 43 8
44 44 8
45 45 9
46 46 9
47 47 9
48 48 9
49 49 9
50 50 10
how many trailing zeros after factorial?
What you are experiencing is a result of this: Why are these numbers not equal?
But in this case, calculating factorials to find the numbers of trailing zeros is not that efficient.
We can count number of 5-factors in a number (since there will be always enough 2-factors to pair with them and create 10-factors). This function gives you trailing zeros for a factorial by counting 5-factors in a given number.
tailingzeros_factorial <- function(N){
mcount = 0L
mdiv = 5L
N = as.integer(N)
while (as.integer((N/mdiv)) > 0L) {
mcount = mcount + as.integer(N/mdiv)
mdiv = as.integer(mdiv * 5L)
}
return(mcount)
}
tailingzeros_factorial(6)
#> 1
tailingzeros_factorial(25)
#> 6
tailingzeros_factorial(30)
#> 7
Efficiently count trailing zeros of numbers from a factorial
Rather than evaluating the factorial computing the length directly, it would be best to use algebraic properties.
For example, rather than accumulating the total, track the number of multiples of two and five.
Number of Trailing Zeros in a Factorial in c
{
long int n=0,facto=1,ln=0;
int zcount=0,i=1;
printf("Enter a number:");
scanf("%ld",&n);
if(n==0)
{
facto=1;
}
else
{
for(i=1;i<=n;i++)
{
facto=facto*i;
}
}
printf("%ld\n",facto);
while(facto>0)
{
ln=(facto%10);
facto/=10; //here you done one mistake
if(ln!=0) //another one here
{
break;
}
else
{
zcount+=1;
}
}
printf("Tere are Total %d Trailing zeros in given factorial",zcount);
}
/Run this code it will work now and mistakes you already knows i guess/
Codechef practice question help needed - find trailing zeros in a factorial
Use the following theorem:
If p is a prime, then the highest
power of p which divides n! (n
factorial) is [n/p] + [n/p^2] +
[n/p^3] + ... + [n/p^k], where k is
the largest power of p <= n, and [x] is the integral part of x.
Reference: PlanetMath
Related Topics
Notification of Object Destruction in Ruby
How to Escape #{ from String Interpolation
Chaining Methods Using Symbol#To_Proc Shorthand in Ruby
How to Use Params with Slashes with Sinatra
No Such File or Directory - Git Ls-Files -- Windows
How Is a Local Variable Created Even When If Condition Evaluates to False in Ruby
How to Dump Strings in Yaml Using Literal Scalar Style
How to Track the Execution Process of Ruby Program
Decrypting Salted Aes File Generated on Command Line with Ruby
Throw Exception When Re-Assigning a Constant in Ruby
Language in a Sandbox in Rails
Rails 3.2 'Link_To' (In Email) with 'Method: :Put' Still Producing Get Request
How to Match Full Words and Not Substrings in Ruby
When Did "Assigned But Unused" Become a Warning for Ruby
Lisp and Erlang Atoms, Ruby and Scheme Symbols. How Useful Are They