## SumoLogic Interview Question

Software Engineer in Tests**Country:**India

**Interview Type:**Phone Interview

better solution for range of primes O(N2) complexity:

```
int main() {
int max = 100;
int min = 0;
int sq = 0;
for (sq = 1; sq*sq<max; sq++){}
if (min <= 2)
for (int i = 1; i <= 2; i++){ cout << i << endl;}
min += ((min % 2 == 0) || (min == 0));
for (int i = min; i < max; i+=2)
{
bool prmFlag = true;
for (int j = sq; j > min - 1; j--)
{
if (j % 2 != 0)
if ((i % j == 0) && (j > 2) && (j != i))
prmFlag = false;
}
if ((prmFlag == true) && (i > 2))
cout << i << endl;
}
}
```

One Good way to Find out Prime Number is

```
public static void primeNumbers(int n) {
boolean flag = true;
if(n%2 == 0) {
flag = false;
} else {
for(int i=n/2+1;i>1;i-=2){
if(n%i == 0) {
flag = false;
}
}
}
System.out.println("\n"+n+" is a Prime Number.. "+flag);
}
```

Please do let me know if someone has a better solution than this .. even recursion is great....

Do you mean to find out if a number is prime or the next prime number? Also i guess you mean O(n) complexity.

- kr.neerav August 06, 2014