lundi 28 août 2017

ques. based on factors and multiples

long long i,N=123456789,j,c;     // A[n]=0:live,=1:killed or disintegrated
//long long c,N;
int A[N]={0};
for(i=N;i>1;i--)
{
    if(A[i]==0)
    {
        A[i]=1;          //kill A[i]
        c++;
        for(j=1;j<(sqrt(i));j++)     //find fac of i & disint. them
        {
            if(i%j==0)
            {
                A[j]=1;        //disintegrate  j
                A[i/j]=1;      //disintegrate n/j

            }
        }
    }
}cout<<c;

Above code is solution to ques. given here [http://ift.tt/2vDClJB]. Code is working good for small inputs but not for large inputs.What changes should I make?

Aucun commentaire:

Enregistrer un commentaire