## Interview Question

Country: United States

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

``````#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

//sqrt logic in terms of int
int x = 2;
while(x*x < n)
{
x++;
}

for(int i = 2; i<= x; 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(25);

}``````

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

``````//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);

}``````

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

\$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;
}

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

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

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

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

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) {
}
}
}
}
int countPrime = 0;
for (int i = 0; i < range; i++) {
if (primeNumArr[i] != 0) {
countPrime++;
}
}
return countPrime;
}

Name:

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

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### 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.