Finding all prime numbers within a given range




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

The forums are wrong (not surprising) and you are correct.

Question for you: Is there is a code size/memory limit? If not, you can precompute everything and hardcode it. It will be super-fast then.

- Anonymous June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ah! I saw it on algorithmist which is a pretty good site otherwise.

There is a limit on memory and code size, but I suppose I can fit it in.

- shahsunny712 July 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The forums are right. You need only calculate primes up to Sqrt(N) to see if any integer up to N is a prime number. Here is a proof:
Case 1: N is a product of prime numbers < n. Trivial.
Case 2: Given some prime number A > r = Sqrt(N) is a factor of N...

N/A = B for some integer B. Rewrite A = (r+i) and B = (r+j). We have N = r^2 = AB = (r+i)(r+j) = r^2 + r(i+j) + ij.

From this we see that j = -ri/(i+1), where r > 0 and i > 0, so it must be that j < 0 for the equality to hold. Therefore, B < r.

B is evenly divisible by a prime number, p < r. And B is a factor of N, thus p must be a factor of N.

Therefore, for any non-prime, N, there must be a prime factor p < r. If you don't find p, then N must be prime.

- John42 August 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a simple solution:

int main() {
	int max = 100;
	int min = 0;
	int sq = 0;

	for (sq = 1; sq*sq<max; sq++){}

	if (min <= 2)
		for (int i = 1; i <= 2; i++){ cout << i << endl;}

	min += ((min % 2 == 0) || (min == 0));

	for (int i = min; i < max; i+=2)
	{ 
		bool prmFlag = true;
		for (int j = sq; j > min - 1; j--)
		{
			if (j % 2 != 0)
				if ((i % j == 0) && (j > 2) && (j != i))
					prmFlag = false;
		}
		if ((prmFlag == true) && (i > 2))
			cout << i << endl;
	}
}

- TheShocker1999 November 30, 2015 | Flag Reply




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