## 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

Problem Types: [Show]

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

• (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 )
"--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'
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.

• (1/2)
What is the largest array possible for this program without exceeding the memory limit?

• (1/0)
You can do the math yourself.

The memory limit is 16MB - that's 16,777,216 bytes.
A boolean is 1 byte, an integer is 2 bytes, and a longint is 4 bytes.

• (0/0)

• (0/0)
In your case it means memory access out of bounds.
What happens in your code if M or N is greater than 5000000?

• (0/0)

• (1/0)
In your particular case it means you are using too much memory.

• (1/2)
but the judge says that i'm only using 64kb of memory for that one

• (2/0)
The memory reading is often inaccurate, and also since your attempt at allocating a huge array fails, the memory usage doesn't include the size of that array anyways.

• (0/0)
The judge's accuracy is roughly +/-0.004s - if you want a faster runtime, optimize your program.
Hint: cin and cout are rather slow.

• (0/0)
What can you replace cin or cout with?

• (0/0)
scanf and printf

• (0/0)
Didn't SourSpinach exceed time limit?

• (2/0)
no it's 2 seconds per test case, so my program could thereotically take 10 seconds. however, I can't take 3 secs on 1 test case and 1 on another.

• (0/0)
It's only difficult if you don't know of the "well known" algorithm for generating primes. It's really quite straight forward once you do, and maybe even unfair for those who don't. (Is a hint perhaps justified?) But that being said, you can still hack it as SourSpinach did.

• (0/0)
Actually, looking at Sour Spinach's code, he still used the same algorithm. But not in the same way as the rest of us.

• (0/0)
is that dissension i am hearing?

• (0/0)
¿Cómo? Did you mean "dissent"?