## Interview Question

**Country:**United States

```
//sqrt calc removed from above logic
#include<iostream>
using namespace std;
void PrintAllPrime(int n)
{
//1 is not counted as price as per my knowledge
int *p = new int[n+1];
for(int i = 0; i <= n; i++)
{
p[i] = 1; //All are prime no
}
p[0] = 0;
p[1] = 0;
// 0 and 1 both are not prime
for(int i = 2; i*i <= n; i++)
{
int mult = 2;
while(i*mult <= n)
{
p[i*mult] = 0;
mult++;
}
}
cout<<1<<" to "<<n<<" all prime no are "<<endl;
for(int i = 0; i <= n; i++)
{
if(p[i] == 1)
cout<<i<<" ";
}
delete []p;
}
int main()
{
PrintAllPrime(30);
}
```

function primeNumbers($n) {

$all_prime_number = array();

for ($i = 1; $i <= $n ; $i++)

{

if ($i == 1) {

$all_prime_number[] = $i;

} else {

$count_divisible = 0;

for ($j = 1; $j <= $i; $j++) {

if ($i % $j == 0) {

$count_divisible++;

}

}

if ($count_divisible == 2)

$all_prime_number[] = $i;

}

}

return $all_prime_number;

}

print_r(primeNumbers(100));

```
function primeNumbers($n) {
$all_prime_number = array();
for ($i = 1; $i <= $n ; $i++)
{
if ($i == 1) {
$all_prime_number[] = $i;
} else {
$count_divisible = 0;
for ($j = 1; $j <= $i; $j++) {
if ($i % $j == 0) {
$count_divisible++;
}
}
if ($count_divisible == 2)
$all_prime_number[] = $i;
}
}
return $all_prime_number;
}
print_r(primeNumbers(100));
```

int getCountOfPrimeNumbers(int range) {

if (range <= 1) {

return 0;

}

if (range == 2) {

return 1;

}

// to store the values starting from 1 to n

int primeNumArr[] = new int[range];

for (int i = 1; i < range; i++) {

primeNumArr[i] = i + 1;

}

primeNumArr[0] = 0;

for (int i = 1; i <= range / 2; i++) {

if (primeNumArr[i] != 0) {

int divider = primeNumArr[i];

for (int j = i + 1; j <= range; j++) {

if (j < range)

if (primeNumArr[j] % divider == 0) {

primeNumArr[j] = 0;

}

}

}

}

int countPrime = 0;

for (int i = 0; i < range; i++) {

if (primeNumArr[i] != 0) {

countPrime++;

}

}

return countPrime;

}

- Satveer Singh June 05, 2014