mardi 24 juillet 2018

Runtime error in Finding perfect square within a range c++

I am trying to solve Sherlock and Square problem in Hackerrank (link) for finding the perfect square within a range of data. But i am getting a runtime error. Please suggest something to improve its performance

My code is as follows:

#include<iostream>
#include<cmath>

using namespace std;

int squares(int a, int b) {
    long double i;
    long long int count=0;
    for(i=a;i<=b;i++)
    {
        long double b = sqrt(i);
        if(fmod(b,1)==0.000000)
        {
            count++;
        }
    }
    return count;
}

int main()
{
    int q,i,a,b;
    cin>>q;
    for(i=0;i<q;i++)
    {
        cin>>a>>b;
        int result = squares(a, b);
        cout<<result<<"\n";
    }
}

Aucun commentaire:

Enregistrer un commentaire