I think you should find no of zeroes at each step and store them. Now you can access the value in O(1). But in ques max value of n should be given.
Like for(i=1 -> n)
at each i it will give i!
For getting Zeros(‘0’) the number should be multiplied by 5 and 2.
since there are so many even numbers between one and the given number, so we need to calculate the number of 5’s, 25’s, 125’s, …, 5^n’s. where 5^n<=N
the formula is N//5 + N//25 + N//125 + …+ N//(5^n)
#include<stdio.h>
int main()
{
long int n;
int t,cur,s;
for(scanf("%d",&t);t>0;t--)
{
scanf("%ld",&n);
for(cur=5,s=0;n/cur;s+=n/cur,cur*=5);
printf("%d\n",s);
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define MAX 1000
long zero(long number,long factor){
long total,deno;
if(number==5) return 1;
total=0;
deno=factor;
while(deno<number){
total+=number/deno;
deno*=factor;
}
return total;
}
int main(){
long N[MAX],c2,c1;
int i,t;
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%ld",&N[i]);
i=-1;
while(++i<t){
c1=zero(N[i],2);
c2=zero(N[i],5);
if(c1<c2) printf("%ld\n",c1);
else printf("%ld\n",c2);
}
return 0;
}
all are wrong i think there is no algo other than brute force