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;
}
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.