## Primes... again

Given two integers N and M (N ≤ M), output all the prime numbers between N and M inclusive, one per line.

N and M will be positive integers less than or equal to 1,000,000,000.
The difference between N and M will be less than or equal to 5,000,000.

`5 20`

### Sample Output

```5
7
11
13
17
19```

Point Value: 15 (partial)
Time Limit: 5.00s
Memory Limit: 32M
Added: Oct 18, 2008

Problem Types: [Show]

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

## Comments (Search)

• (1/0)
I tried using Sieve of Eratosthenes but I cannot seem to find the error in this program... I understand it's harder in python but does anyone know what exactly is causing the errors? Thanks :)

• (3/0)
N and M are at most 1,000,000,000. Any large test cases around that size will require way too much memory to store and there's no way you'll ever be able to do it like that. This problem is worth 15 points, which is a lot more than the average prime problem. This is due to the fact that this cannot be done using the naive way, and you would have to chance your algorithm a bit.

• (0/0)
I'm getting runtime error.
Does this mean that I timed out? What does it mean?
(It would be helpful if I could see the error message)

• (0/0)
Try resubmitting. You should be able to see what kind of runtime error it is now.

• (0/0)
i know how to do this but the largest ordinal thing is longint...

• (2/0)
Couldn't you also use Int64? and why would you need anything that large anyways?

• (0/1)
what is SIGSEGV?

• (1/0)
You may want to read the 'help' section.

• (0/0)
The time limit per case has been increased to 5 seconds so that Java users can solve this problem.

In other languages, any solution that times out with 2 seconds will still time out with 5 seconds.

• (0/0)
Can't you have a language specific time limit? E.g. slower langs like Java and anything interpreted could have a different time limit for problems (say 1.5x or something). This would also avoid having to manually redo all the other problems impossible to do in time limit which invariably exist.

• (0/0)
No, it would just be far too much trouble. If we increase the time limits by a factor of 1.5 for Java, what should the factor be for Python? PHP? Ruby? Perl?

In my opinion, if you're not using a language that uses almost every machine instruction doing calculations (C, C++, Pascal, assembly) then you're already doing something wrong.

• (0/0)
I applied a variation of sieve of Eratosthenes. It takes approximately 3.9 seconds on the last case ? How can I make it faster ?

• (0/0)
A hint I can give is to try to combine your init() procedure with your main program. Output optimisations are not required in this problem.

• (0/0)
I am sorry I don't get it. I mean, did everyone use Sieve of Eratosthenes or something else. Like How do I integrate init( ) with the program ?

• (1/0)
Yeah, from just a quick glance, every accepted submission makes use of the Sieve of Eratosthenes.

As Daniel said, with the right idea, output optimisation is unnecessary and over-complicates your code.

Here's a hint: If you know all the primes up to some number M, you can determine all the primes up to some number N. How are M and N related? (Yes, your code implies you already know this; you're just not applying it in the best way.)

• (0/0)
Thanks to Daniel and jargon.Accepted.

• (0/0)
So first you use the sieve of Eratosthenes and then...?

• (0/0)
The Sieve of Eratosthenes finds prime numbers. So I'm not sure what you mean by "and then"... all you do is print the prime numbers you find. You do need to implement a variation on the standard Sieve though, otherwise you'll need a boolean array of size one billion and your program will run out of time anyway. Once you figure out what that variation is, the problem should be easy. If you're stuck, try solving other problems and then coming back to this one later.

• (0/0)
Well, can you give me a clue on the variation?
Is it, a), an integer,
b) string,
c) char?
An array as well, right?

• (1/0)
I recommend that you try this problem later when you have more coding experience. You should at least try coding the original Sieve, anyway, and see whether you can get Primes (1) accepted.

• (0/0)
Can't you use an array of [1..5000000] of boolean for this question instead?

• (0/0)
Yes, you should. This is not necessarily the only array you need to use, though.

• (0/0)
You can use an array like that, it's just small enough to fit into the memory allowance. However, not just any solution will run in time.

• (0/0)
Anyone know why you can't set an array of 5000000 bools in MinGW on Code::Blocks 10.05? Gives general SIGSEGV fault, but works fine on judge.
Is it a Windows limitation? I thought I could create arrays of 5MB on Free Pascal...

• (2/0)
That's because memory on the stack is limited. There's much more on the heap.
On windows, you could try (assuming mingw uses gnu gcc compiler )
Go to CodeBlocks->Settings->Compiler and Debugger->Linker settings->Other linker options and add
"--stack,16777216" (no quotes) to increase stack size to 16MB (or use any other reasonable value)
Then your code should work, unless you want 20 million booleans.

not that stack size is different from the total memory available to the program
(you probably have at least a gigabyte of memory available on your own computer.)

Or you could've just written your program in one of the following ways.

Make a global boolean array of size 5 million

or
#include <vector>
...
vector<bool> isPrime (5000000); //(may cause frustration in some cases)
or
bool*isPrime = new bool[5000000];
...
delete []isPrime;

• (0/0)
I am getting runtime error. Is it because I am exceeding the memory limit.

• (0/0)
Yeah, executing your program on the terminal gives
"terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted"
However, even when the memory limitation is removed, it TLEs.

• (2/0)
there is a "well-known" algorithm for generating prime numbers up to N, as Dr. Sane pointed out. Try finding it.