Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BooleanArray with size larger than 2147483647

I am trying to implement the sieve of Atkin in Kotlin. I want it to support numbers up to 2^32-1, so the sieve must be a UInt-indexed array.
I try to initialise the sieve like this:

var sieve = BooleanArray(limit + 1u)

Then, I get the error:

error: type mismatch: inferred type is UInt but Int was expected

So, is there any way of making a BooleanArray (or equivalent) store at least 4294967295 values?

like image 554
some random nerd Avatar asked Oct 15 '25 04:10

some random nerd


1 Answers

The most easy might be to use UInt as the size and then internally map 2 common BooleanArray, which use Int for addressing, despite there never could be a negative index value (this seems to be a design fault - or at least a lack of optimization). Which means, that one could as well address everything with signed Int. I mean, to internally map the negative values to one BooleanArray and the positive values to another one BooleanArray. The actual problem seems to be, that a signed Int is being passed, but only the positive range (50%) can be used to address data. One could use absoluteValue, because it doesn't matter in which direction the array is being filled.

like image 196
Martin Zeitler Avatar answered Oct 17 '25 09:10

Martin Zeitler