DSA Learning: FCTRL

https://www.codechef.com/LRNDSA01/problems/FCTRL

I had a doubt in this problem. What’s wrong with my approach? It is giving correct answer for all input test cases but shows wrong answer.

t = int(input())
for i in range(0,t):
    n = int(input()) 
    digit= 0
    numb = n
    summ=0
    while(n!=0):
        n=n//10
        digit+=1   
    print(digit)  
    j=1
    while(j!=0):
        x = (5**j)
    #     print(5**j)
        j+=1
        summ=summ+(numb//x)
        a = str(x)
        if(len(a)==digit):
            break
    print(summ)

Submission link: https://www.codechef.com/viewsolution/33535115

1 Like

Why that

2 Likes

You just have to count the number of 5's from one to n
which can be done in a simple loop:

This is your answer:

int n,ans=0;
cin>>n;
for(int i=5;i<=n;i*=5)
{
	if(n/i>0)ans+=n/i;
	else break;
}
cout<<ans<<endl;

PS:You can easily convert it to python.

1 Like

for _ in range(int(input())):
n=int(input())
cnt=0
i=5
while (n//i>=1):
cnt+=(n//i)
i=i*5
print(cnt)

Yes, thanks for asking me this. I figured it out where I was going wrong. Thanks man.

Yes I had certainly figured out this logic after spending some time. But I wasn’t able to understand what was wrong in my first approach. I got it though. Thanks to the question asked by @galencolin. Anyways, thanks for the help guys @zephxr @pratik121 . :slight_smile:

1 Like

:+1: