Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create array of size greater than integer max [duplicate]

I was trying to get all primes before 600851475143. I was using Sieve of Eratosthenes for this. This requires me to create a boolean array of that huge size. Bad idea, you can run out of memory. Any other way. I tried using a string, using each index with values 0 & 1 to represent true or false. but indexOf method too returns int.

Next i am using 2d array for my problem. Any other better way to store such a huge array?

like image 721
S Kr Avatar asked Apr 15 '13 12:04

S Kr


1 Answers

The memory requirement for 600851475143 booleans is at best 70Gb. This isn't feasible. You need to either use compression as suggested by Stephan, or find a different algorithm for calculating the primes.

like image 156
Alan Avatar answered Sep 24 '22 19:09

Alan