1. 3

  2. 4

    I think it’s unlikely that is the fastest way to test for primes in java.

    1. 1

      It also has the amusing characteristic of crashing if your number is larger than the largest contiguous span of free memory in the JVM heap.

      1. 1

        The JVM heap can keep growing as long as the OS allocator never says no.

        Virtual memory compression at the OS level should be able to compress strings of char[n] to char[lg(n)] with run-length encoding.

        (Well, in theory. I never tried running it :P)

        1. 3

          You’ll run into a much tighter, much stupider limit much sooner than that becomes relevant: the longest possible Java array only has 2 billion-ish elements (2^31-1, to be precise, because Java arrays are indexed by Java ints, which are 32 bit and signed). I can comfortably fit the largest possible char array—4GB—uncompressed in memory on every computer I use regularly.

          1. 1

            No, it can’t. The JVM has a maximum heap size set at start up, which defaults to 1GB or less. And even if the OS compresses that memory, the JVM allocator still counts the uncompressed size.

            1. 1

              Ah ok. I guess -Xmx999999g is in order.

        2. 1

          Sounds like the regex compiler has its work cut out.

          Naw, this is just for fun :)

        3. 1

          No need for the redundant programming tag here.