## MAGMA Interview Question for Software Engineer / Developers

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

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

printf("Given number 'n' is ");
for(i=2;i<n/2;i++)
{
if(n%i==0)
{
printf("NOT ");
break;
}
}
printf("a prime number");

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

(((Any prime is of the form 6*n+k (k=1,5)}}}

Am I understanding it correctly?

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

dude...it meant any prime can be represented as 6n+1 or 6n-1 and not that any number which has this form is prime.Any way i think he made a mistake saying"So set all 6*n+(1|5) as a prime."

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

@Sathya: Its said right but the statement was not complete. I changed it. So we basically set all 6*n+(1|5) as a primes and then eliminate their multiples so we should be left with primes in the end. for example: For all primes below 50
set 5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49
eliminate their multiples. i.e.
-> eliminate multiples of 5: 25,35
-> eliminate multiples of 7: 49
so on... we should be left with primes in the end

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

ya...that sounds good...btw i read only the quote at first

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

you mean Dijkstra's quote?

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

@PKT:
No need to traverse until n/2
sqrt(n) will be sufficient

modified code

``````#include "math.h"

printf("Given number 'n' is ");
for(i=2;i<sqrt(n);i++)
{
if(n%i==0)
{
printf("NOT ");
break;
}
}
printf("a prime number");``````

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

Also, don't bother checking even numbers. (ie: check if n is even before starting the loop, then use for(i=3;i<sqrt(n);i+=2) ). This is called the Seive of Erasthones (I think).

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

yup,go on dividing by natural numbers till the divisor reaches sqrt(n)[This answer is for the non-programming people]

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.