## EMC Interview Question Software Engineer in Tests

• 0

Write 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

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````static 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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}

Comment hidden because of low score. Click to expand.
0

Can you please explain why you need array? The way I see it solved is loop till SQRT(N) if loop counter is prime increment j and return j.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.