## Riverbed Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

sieve of eratosthenes comes handy.

``````boolean isPrime(long n) {
if(n < 2) return false;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}``````

Impls is copied from stackoverflow(good impl).

You can read the algorithm wiki

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

Depending on how you choose to solve this, the problem can be anything from trivial to being one that had eluded computer scientists for many decades.

The AKS Primality Test, published in 2002, provided the first deterministic and provably polynomial-time (in the number of digits) general algorithm for checking whether a number is prime. Again, this was only discovered in 2002. It took that long for this to be figured out.

On the other hand, if you want to give an inefficient but simple algorithm, you can just try dividing the input by every integer >=2 and <= the square root of the input. Your input is prime if and only if it's not divisible by any of those integers.

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

check if the number is divisible by 2..n/2.

``````for (x=2; x<n/2; x++)
if (N%x==0) break;
if (x==n/2) printf("it's a prime");``````

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

``````if (x>2) return false;
for (int i = 2; 2*i <num; i++){
if ( num % i ==0) return false;
}
return true;``````

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

``````if (x<2) return false;
for (int i = 2; 2*i <num; i++){
if ( num % i ==0) return false;
}
return true;``````

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

Figured out a way rather a 'Hack' in python to find out given number is prime or not. Please pass on your feedback

import math

def isprime(n):
if n == 2: return print('You got the First Prime number')
while (n > 2):
x, y = math.modf(math.sqrt(n))
if (x == 0.0): return print ('Given number %s is NOT a Prime Number' % n)
else:
return print ('Given number %s is a Prime Number' % n)

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

Forgot to preserve the code

``````import math

def isprime(n):
if n == 2: return print('You got the First Prime number')
while (n > 2):
x, y = math.modf(math.sqrt(n))
if (x == 0.0): return print ('Given number %s is NOT a Prime Number' % n)
else:
return print ('Given number %s is a Prime Number' % n)``````

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

Sorry, I was naive to think it covers all prime numbers but it is not after testing. Incorrect. pls ignore

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

Sry for spamming, i'm giving one last try. The codes does seem to identify 'most' prime numbers that I passed for testing. The problem is that the code does identify some non primes as primes.

Note: My testing was Limited

``````import math

def isprime(n):
if n == 2: return print('You got the First Prime number')
while(n > 2 and (n % 2 is not 0) and (n % 3 is not 0) and (n % 5 is not 0) and (n % 7 is not 0)):
x, y = math.modf(math.sqrt(n))
if (x == 0.0): return print ('Given number %s is NOT a Prime Number' % n)
else:
return print ('Given number %s is a Prime Number' % n)

return print ('Given number %s is NOT a Prime Number' % n)``````

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.