MAGMA Interview Question for Software Engineer / Developers






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

www dot optionsbender.com/technologybending/algorithms/primenumbers

- blueskin.neo February 17, 2011 | Flag Reply
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");

- PKT February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@blueskin.neo.. about the equation mentioned in your link
(((Any prime is of the form 6*n+k (k=1,5)}}}
what about n=10 & k=5?

Am I understanding it correctly?

- johnny February 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Sathya February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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

- blueskin.neo February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Sathya February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you mean Dijkstra's quote?

- blueskin.neo February 19, 2011 | Flag
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");

- Dude March 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous April 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- canmpuppala January 28, 2012 | Flag


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More