FACTORIAL running absolutely fine on code blocks but here it is giving error?

http://www.codechef.com/problems/FCTRL/
This program is running fine on my pc but here it is showing wrong answer,where is the error?

//droftware
#include<stdio.h>
#include<math.h>
int main()
{

int t,q,i,lo,sum=0,j,d,z;
float f,l,fi;

f=log10(5);
scanf("%d",&t);
int arr[t];
int brr[t];
z=5;
for(i=0;i<t;++i)
{
    d=0;
    scanf("%d",&arr[i]);
    l=log10(arr[i]);
    lo=l/f;

    for(j=1;j<=lo;++j)
    {

        fi=pow(z,j);

        d=arr[i]/fi;

        sum+=d;

    }

    brr[i]=sum;
    sum=0;
}

 for(i=0;i<t;++i)
 {
     printf("%d",brr[i]);
     printf("\n");
 }
 return 0;

}

First suggestion is that all outputs should be on different line. see output format. It still gets rejected(WA) after correcting this.

Floating point comparison is the cause of the problem here.

Just changing the condition to exit from the loop gets your solution AC.Here is the corrected version

1 Like

@droftware : The problem is not with logic but with precision .

You are unnecessarily doing floating point arithmetic , which is not needed in this problem

Using double may help since that is higher precision , but it may still fail , since double will also not be exact precision .

For your information , float is 7 digits precision and double is 15 digits precision .

You can do this question purely on integer arithmetic .
.
for(j=1;j<=lo;++j) : You should change this loop

power = 1

while(power <= (arr[i]/5) ) {

power *= 5;

d = arr[i]/power;

sum += d;

}

This will give you correct answer .

Now coming back to why you get wrong answer . Suppose n = 125 and your log calculations give , lo = 2.999999999999… something . Then loop will not run for lo = 3 , which it should run .

1 Like