Ineffective code!

I am fairly new to programming and are trying out some of the easy practice problems on codechef. I did this problem called Factorial( link: http://www.codechef.com/problems/FCTRL) and my submission succeeded, but i couldnt help noticing that my implementation was considerably slower than the most effective ones. 60 times slower in fact. To me, my code seemed pretty straight forward, no immense growth factors or anything like that. Here is my code:

#include<iostream>

using namespace std;

int main(){
ios_base::sync_with_stdio (false);

int t,n;
cin>>t;

for (int i=0; i<t; ++i){
    int p=5;
    int c=0;
    cin>>n;

    while(p<=n){
        c=c+n/p;
        p=p*5;
    }
    cout<<c<<'\n';
}
return 0;

}

So i wondered if there is any obvious statements in my code that hinder my performance greatly? Is it the way i handle I/O?

Hello @ancientninja,

You can actually optimize your I/O handling further by using the scanf()/printf() C input/output routines instead of the C++ ones you are using… That alone should give you a big boost in performance I think… (Unless the line ios_base::sync_with_stdio (false);makes some difference but I’m not sure)

To go to the maximum extent you can refer to the fastest I/O routines used on the top submissions and which were discussed here on the boards quite often as well :smiley:

Best regards,

Bruno

yes 3 times of ur input time will reduce…

U can also use a fastread function with getchar or getchar_unlocked. With the same approach as yours and with the use of getchar_unlocked my submission time is 0.11s.

try using fast i/o maybe it can speed up your program