Vector memory allocation problem. Is anything wrong with vector container to allocate big memory?

Here’s a seive to find composites .It works fine if I input <=19000 but for bigger values the program doesn’t work…
Is there any problem with vector…although vector max size is much bigger.But why it collapses?Then

I’m really in a fix what to do…What container is best to store very large data…
I don t want to use array .It just kills space …
What container ll be best for this ?

and do i need to use any other data structure to get big space?
Thanks :slight_smile:

Your program, rather the logic, is wrong. It marks squares of prime numbers greater than 4 as prime itself.

That is, numbers like 25 (=52), 49 (=72), 121 (=112), 169 (=132), ... are not printed in this list.

Please check your program for this. And I think, your vector is OK.

This is Sieve of Eratosthenes, isn’t it? Please re-check the implementation’s correctness. Here is how it is done (in pseudocode).

1 Like

Oh! It seems that, not just the squares, but all the powers of primes greater than 4 are getting marked as prime. 125 (=53) is missing. So is 343 (=73) and 625 (=54) and 1331 (=113) and a lot more.

1 Like

Hi, your program is correct.

If you look at the input/output description (in your ideone code), you are getting a runtine error (SIGXFSZ). This just means, you have exceeded the allowable output file size on ideone.

If you run this on your machine, you will get your complete output.

As a proof of what i said, when you are displaying output, just displaying the number (without any messages like “10th composite is:”) will show results upto about 14200.

So, YOUR PROGRAM IS CORRECT! Cheers!

1 Like

Thanks a lot for your help :slight_smile:
everything seems okay with 2,3 but i’ve marked 5 as prime simply in the loop and c should increment (when it reaches 5[no reason to not to reach]5*5=25)…I dont get where i ve made mistake…same with powers of 7,11,13 …

I’ve corrected the code :slight_smile: But still see,after 32000 no composites are printed and i also got a run -time error… Can you help me in this …Why actually i cant get all composites printed.

Thanks man,You helped a lot :slight_smile: