FCTRL time limit xceedd ..

how can i optimise my code such dat it dont xceed the time limit …q. is fctrl

#include
#include<stdio.h>
#include<stdlib.h>

using namespace std;

    int fact(int n){

       int i,x=0;

  for(i=n;i>=1;i--)
            {
                n=n/5;
                x=x+n;
            }
            return x;
     }

      int main()
 {
int t;
int n;
scanf("%u",&t);
    while(t--)
    {
            scanf("%u",&n);
            printf("%d",fact(n));
            printf("\n");



    }

return 0;

}

Your code seems fine to me, but one thing you can try is to replace

scanf("%u",&t);
scanf("%u",&n);

with

scanf("%d",&t);
scanf("%d",&n);

that’s the only part that “smells”…

edit: this is not the reason, hint is in another comment…

1 Like

stil showing time limit xceedd …taking too much time for n=1000000000 while executing in my compilr

I tried this one, but didn’t realized the problem…

Try to think about it, why are you using

for(i=n;i>=1;i--)

it is really needed to execute the loop million times for n = 1.000.000 ?

remove the conditions

if(t<=100000)

and

 if(n>=1 && n<=1000000000)

the second one is correct, you can replace it with assert…

hmmm ur ryt …instead v can write

while(n>=1)
1 Like

…in fact n >= 5, but this is a tiny little detail :wink:

yes ur ryt …m a newbie so still learning …nyways thanks

Please accept and/or upvote the answer if it has solved your doubt.