Why this code is getting "run time error :Last executed input: 7"?


  • 0
    Z
    void setisPrimes(int i,int n,int *isPrimes)
        {
            for(int index=i*i;index<n;index+=i)
               isPrimes[index]=0;
            
        }  
        int getnextPrime(int i,int n,int *isPrimes)
        {
            for(int ii=i+1;ii<n;++ii)
              if(isPrimes[ii])
                return ii;
            return -1;    
        } 
        int countPrimes(int n) {
            int *isPrimes=new int(n);
            memset(isPrimes,1,sizeof(int)*(n));    
            isPrimes[0]=isPrimes[1]=0;
            int i=2;   int count=0;
            
            while(i<n && i!=-1)
            {
                count++;
                setisPrimes(i,n,isPrimes);
                i=getnextPrime(i,n,isPrimes);
               
            }
            return count;     
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.