0's of a factorial

I want to make a program which takes a number n and then outputs how many 0's the factorial has on the right site of the factorial.

For this, I thought about the following algorithm:
I noticed, every 5th number gets a 0 more. So, 1->0,5->1,10->2 and so on.

In this program I divide the number by 5 and subtract its modulo to it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main(){
    int casos;
    std::cin>>casos;
    int num=0;
    long result;
    for(int n =0;n<casos;n++){
        result=0;
    std::cin>>num;    
    result=(num-num%5)/5;
    if(num==0){
        result=1;
    }
    std::cout<<result<<"\n";    
    }
    return 0;
}
So, what exactly is your question?

BTW: https://oeis.org/A027868
The program does not work... The checker invalids it
Well, the most obvious thing is that 0! is 1, which has 0 zeroes.
The problem indicates, that the result has to be 1 if the num equals 0.


Además, por convenio, el factorial de 0 es 1 (es decir, 0! = 1).

I don't know if that means just the factorials or also the 0's. But the checker does not validate it even without the 0 part
Last edited on
That isn't what that is saying. You're still trying to calculate the number of trailing 0s. 0! (1) has zero trailing 0s.

Look at the link I posted. Compare your results to the sequence.
hint: 25!

Moral of the story: Patterns with early numbers don't always last forever.
Last edited on
Ok. But the program does not work also if 0 has 0 0's
doesn't matter if it works for anything, the math is wrong.
when you do 100!, *100 immediately adds 2 zeros. when you do 1000!, it immediately adds 3 zeros. And so on. if you are only doing to 99! or less, then you may be able to fit a partial pattern, but if you are only doing to 99 or less, just dump the answer in a lookup table.
@yabi2943993,

You do know that @dutch gave you the exact solution and code here:
http://www.cplusplus.com/forum/general/267490/#msg1150931

As @jonnin said: your maths is wrong. There are more increments than simply increasing by 1 every multiple of 5.
Oh ok. Haven't seen dutch's comment
Topic archived. No new replies allowed.