Help needed in Factorial question(FCTRL(easy practice questions))

I keep getting a runtime error in my program. Please tell me what all errors am I doing.

Code-:

#include < iostream>
using namespace std;

int main() {
unsigned long int lines,number[1000000000],zeroes=0,bak;
cin>>lines;
for(int i=0;i<lines;i++)
cin>>number[i];
for(int i=0;i<lines;i++)
{ zeroes=0;
for(int j=1;j<=number[i];j++)
{
if(j%10==0)
{
zeroes++;
continue;
}
if(j%5==0)
{ bak=j;
while(bak!=1)
{
zeroes++;
bak=bak/5;
if(bak%5!=0)
break;

                  }
              }
          cout<<zeroes<<'\n';
              
          }
    }

return 0;
}

I think one of the obvious mistakes is that your array is too big.

1 Like

Problem Link?



1 Like

Array overflow maybe.

1 Like

Hey, thanks for answering.

In the input section, it says There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.

How can I get this without the use of a large array?

Problem-FCTRL Problem - CodeChef

My solution-:CodeChef: Practical coding for everyone

we never need to store these inputs in array , just run a loop for a test cases, take input in normal variable and give a output then go for next test case do the same process or i will advice you once to check the code of others of any different question.

In fact, you don’t need to save all the 1-n numbers.

And if you want to output all at once . Just tie them together.
cin.tie(0);

Then I suggest you find the implicit mathematical law of the problem. The problem is to find the number of zeros at the end of the factorial of n. You are very smart. You know how many factors are 5 in the number of 1-n, so the answer is very obvious.
Instead of counting one by one, consider the whole.

3 Likes

For example if n is 54, then no. of 5’s in [1,54] are {5,10,15,20,25,30,35,40, 45,50} i.e.10, 54/5, but 50 and 25 itself has 2 5’s, so there are total 12 5’s, which is 10 + 2 i.e. 54/10 + (54/10)/10.
For eg: how many 2’s are in 8!? numbers having 2 are {2,4,6,8} but 4 and 6 have 2 2’s so ans is 6 i.e. 8/2 + (8/2)/2…
This is general way by which you can find multiples of x in range[a,b]. So you can apply it here.
Ans will be b/x + b/x*x…upto b!=0 and minus one which are in before a using same thing.
I did this problem in spoj and took couple of hours getting this :rofl::rofl:

1 Like

I tried doing this(CodeChef: Practical coding for everyone). Now after submission it says time limit exceeded.

If you read @codingisez’s answer, then you will understand it.

1 Like

Damn, just focus on num, num/5 + num/5/5 … will give multiple of 5 from 1 to num inclusively,
Here is little snippet

while(num!=0){
cnt+=num/5;
num/=5;
}

Here cnt is variable.
P.S. don’t make habit of defining count as variable
If still you find it hard read editorial, it will explain more briefly

1 Like

@freeloop @codingisez @tchalla @shivamchef Thank you for your help. Finally solved it! :smile:

ur welcome :slight_smile:

#include <stdio.h>
int F(int);
int Z(int);
int main()
{ int N,fact,ans;
printf(“enter the N”);
scanf("%d",&N);
fact=F(N);
ans=Z(fact);
printf("%d",ans);
return 0;
}
int F(int x)
{
if(x==0||x==1)
return 1;
else
return x*F(x-1);
}
int Z(int x)
{int rem,count=0;
while(x!=0)
{rem=x%10;
x=x/10;
if(rem!=0)
break;
count++;
}
return count;
}
Why it is wrong even it is working