EMC Interview Question Software Engineer in Tests
0of 0 votesWrite a function that counts the number of primes in the range [1-N]. Write test cases for this function.
Team: RSA
Country: India
Interview Type: Written Test
int fun(int N)
{
int array[N] ={0};
int i= 2,j;
while(i<sqrt(N))
{
j =i;
j=j+j;
while(j<N)
{
array[j] = -1;
j = j+i;
}
i++;
}
j=0;
for(i=0;i<N;i++)
if(array[i]==-1)
j++;
return(j);
}

- sqw on June 04, 2012 Edit | Flag Replystatic int getNumberOfPrime(int N) { int count = 0; for (int i=2; i<=N; i++) { int max = (int)Math.sqrt(i); boolean prime = true; for (int j=2; j<=max; j++) { if (i%j == 0 && i != j) { prime = false; break; } } if (prime) { count++; System.out.print(i + ","); } } return count; }