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.
Sample Input
5 20
Sample Output
5 7 11 13 17 19
All Submissions
Best Solutions
Point Value: 15 (partial)
Time Limit: 5.00s
Memory Limit: 32M
Added: Oct 18, 2008
Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3
Comments (Search)
Is it a Windows limitation? I thought I could create arrays of 5MB on Free Pascal...
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;
"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.
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.
What happens in your code if M or N is greater than 5000000?
Hint: cin and cout are rather slow.