• 0

Country: India
Interview Type: Written Test

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

#include<bits/stdc++.h>
using namespace std;
int prime(int n)
{
int flag=0,i;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
flag=1;
}
if(flag==0)
return 0;
}
int main()
{
int a,i=0,j,k,n,b;
cin>>a;
cin>>n;
while(i<=n)
{
b=++a;
j=prime(b);
if(j==0)
i++;
if(i==5){
cout<<b;
break;
}
a=b;

}
}

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

C++, Caching

``````#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

vector<int> primes;

void initPrime(int a) {
int i, j, l;
bool p;

for (i = 2; i <= a; i++) {
l = (int)sqrt((double)i);
p = true;
for (j = 0; j < primes.size(); j++) {
if (l < primes[j]) break;
if (i % primes[j] == 0) {
p = false;
break;
}
}
if (p) primes.push_back(i);
}
}

bool isPrime(int n) {
int i, l;

if (n < 2) return false;

l = (int)sqrt((double)n);
for (i = 0; i < primes.size(); i++) {
if (l < primes[i]) break;
if (n % primes[i] == 0) return false;
}
primes.push_back(n);

return true;
}

int solve(int a, int n) {
initPrime(a);

while (n) {
if (isPrime(++a)) n--;
}

return a;
}

int main(int argc, char* argv[]) {
int a, n;

cin >> a >> n;
cout << solve(a, n) << "\n";

return 0;
}``````

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

Java code:

``````public class PrimeNumber {

public void print(int a, int n) {
while (n != 0) {
if (isPrime(++a)) {
--n;
}
}
System.out.println(a);
}

private boolean isPrime(int a) {
if (a == 2) {
return true;
} else if(a < 2 || a % 2 == 0) {
return false;
}
for (int i = 3; i*i <= a; i += 2) {
if (a % i == 0) {
return false;
}
}
return true;
}

}``````

Quadratic time, constant space complexity. If we have maximum limit for output prime number, we could use Sieve of Eratosthenes to calculate all prime numbers beforehand. Thus, complexity would be O(n log log n) time and O(N) space.

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

A prime number could be in format of 6n+-1 so check for numbers in this format if they are prime or not upto the given count.

``````int c = 0;
int n = 1;
int lastPrime=-1;
while( c < N ) {
int k1 = 6*n-1;
int k2 = 6*n +1;
if( isPrime(k1) )	{ lastPrime = k1; c++; }
if(c == N)	break;
if(isPrime(k2) )		{ lastPrime = k2; c++; }
n++;
}
return lastPrime;``````

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

<?php

\$handle = fopen ("php://stdin","r");
fscanf(\$handle,"%d",\$n);
//var_dump(\$n);
for(\$i=1;;\$i++)
{ \$n=\$n+1;
// var_dump(\$n);
if(\$n%2!=0){
\$p[]=\$n;
if(count(\$p)==5)
break;
}
}
var_dump(end(\$p));

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

``````<?php

\$handle = fopen ("php://stdin","r");
fscanf(\$handle,"%d",\$n);
//var_dump(\$n);
for(\$i=1;;\$i++)
{  \$n=\$n+1;
// var_dump(\$n);
if(\$n%2!=0){
\$p[]=\$n;
if(count(\$p)==5)
break;
}
}
var_dump(end(\$p));``````

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.