Computer Science Canada

Array length of a long value

Author:  Naveg [ Wed Nov 02, 2005 6:45 pm ]
Post subject:  Array length of a long value

Is it at all possible to have an array that has a length of a long value?? So far all my research has shown that only ints can be the index of an array....

If not, how would I go about storing a "long" number of things?

Author:  wtd [ Wed Nov 02, 2005 7:01 pm ]
Post subject: 

ArrayList also appears to be limited to "int" length.

I'd suggest reconsidering why you're storing that much at one time. That would be using massive amount of memory.

Author:  Naveg [ Wed Nov 02, 2005 7:22 pm ]
Post subject: 

well, i have an assignment to find all the primes up to an inputted number...and i thought i would use the sieve of eratosthenes. Use a boolean array, start all at true, gradually eliminate composite numbers, output all remaining "true" indexes

Author:  wtd [ Wed Nov 02, 2005 7:36 pm ]
Post subject: 

Do you expect such a large input number?

Author:  Naveg [ Wed Nov 02, 2005 9:10 pm ]
Post subject: 

My teacher said to use longs so he can test the efficiency with larger numbers.

Author:  wtd [ Wed Nov 02, 2005 9:38 pm ]
Post subject: 

Then you may have to try a different strategy. Smile

Author:  Naveg [ Wed Nov 02, 2005 10:20 pm ]
Post subject: 

Well...would there be any other way to implement the sieve of eratosthenes without using an array?

Author:  beard0 [ Wed Nov 02, 2005 11:21 pm ]
Post subject: 

Naveg wrote:
Well...would there be any other way to implement the sieve of eratosthenes without using an array?


No. You could however use the sieve of eratosthenes using a two dimmensional array where

code:
virtualArray[longValue] = actualArray[longValue / maxInt][longValue % maxInt]

Author:  GlobeTrotter [ Wed Nov 02, 2005 11:37 pm ]
Post subject: 

What about using a linked list?

Author:  rizzix [ Thu Nov 03, 2005 1:53 am ]
Post subject: 

GlobeTrotter wrote:
What about using a linked list?
That would slow the algorithm down... the faster alternatives would be ArrayList or Vector.. but then again.. i think they too fall under the same restriction.

Author:  wtd [ Thu Nov 03, 2005 1:59 am ]
Post subject: 

rizzix wrote:
GlobeTrotter wrote:
What about using a linked list?
That would slow the algorithm down... the faster alternatives would be ArrayList or Vector.. but then again.. i think they too fall under the same restriction.


They do.

Author:  Naveg [ Thu Nov 03, 2005 5:31 pm ]
Post subject: 

how then do people have programs that can find primes up to massively huge numbers like 100000000000 using the sieve of eratosthenes? Do they not use arrays?

I'm researching a different algorithm for finding primes to try and accomodate a larger range of values without sacrificing speed....

Author:  wtd [ Thu Nov 03, 2005 5:36 pm ]
Post subject: 

Naveg wrote:
how then do people have programs that can find primes up to massively huge numbers like 100000000000 using the sieve of eratosthenes? Do they not use arrays?


They don't use Java?

Author:  Naveg [ Thu Nov 03, 2005 5:45 pm ]
Post subject: 

wtd wrote:
Naveg wrote:
how then do people have programs that can find primes up to massively huge numbers like 100000000000 using the sieve of eratosthenes? Do they not use arrays?


They don't use Java?


Figured you'd say that. But in a language like C++, do arrays take up less RAM? Or are the other data structures in C++ just far faster than those of Java?

Author:  wtd [ Thu Nov 03, 2005 5:48 pm ]
Post subject: 

Well, in C and C++, the problem is that arrays are glorified pointers. On a 32-bit machine, array indexes can only be 32-bit integers.

Author:  [Gandalf] [ Thu Nov 03, 2005 5:48 pm ]
Post subject: 

What makes you think he was talking about C++? Maybe he was, but I'm sure you could make some even faster optimised Assembly code. Try to keep your mind open on things like this.

Author:  rizzix [ Thu Nov 03, 2005 5:57 pm ]
Post subject: 

it's simple actually... here's how you'd implement it..

#1: use a multi-dimensional rectangular array (let's take a 2D array, as a 2D array will provide for storing a maximum of (2^32 - 1)*(2^32 - 1) values... *a huge number!!*
#2: fix the ROW and COL dimension to MAX_INT
#3: create a class.. that will handle the 1D -> 2D conversion for you.


now i'm not going to create this class for you but... here's the basic mechanics behind such a class:
code:
long[][]  primes = new long[Integer.MAX_INT][Integer.MAX_INT];


basically the Nth value in the emulated 1D-array is the (row*Integer.MAX_INT+col)th value in the 2D "primes" array. so in ur long get(long n); you should basically have
code:
**refer to beard0's post**
(edit: sorry dude didn't see ur post)

note: the code is not tested... it could be wrong.. but hopefully you get the idea Wink
also note: long may not be a large type to encompass all those values.. -,-'' You might have to use the BigDecimal class.. Or even better reduce the size of the 2D "primes" array.

Author:  Naveg [ Thu Nov 03, 2005 6:10 pm ]
Post subject: 

Well the total size of the array only needs to be a number specified by the user. As well, the array type can be boolean (ie. is the index of this value prime of not).

Author:  rizzix [ Thu Nov 03, 2005 6:12 pm ]
Post subject: 

ah well all the better then.. but you get the idea..

Author:  beard0 [ Fri Nov 04, 2005 3:44 pm ]
Post subject: 

Err... did nobody read my post about 2D arrays on the previous page?

Author:  wtd [ Fri Nov 04, 2005 3:50 pm ]
Post subject: 

Hey, you know what might work? Using a two dimensional array.

Wink

Author:  rizzix [ Fri Nov 04, 2005 4:42 pm ]
Post subject: 

there's a way to do it with a 2D array?? cool!! how? Razz

Author:  Naveg [ Fri Nov 04, 2005 4:43 pm ]
Post subject: 

would it though? In theory it would yes, but I'm still restricted by java's memory limitations...which are currently reached even before an array of MAX_INT values.

Author:  rizzix [ Fri Nov 04, 2005 8:41 pm ]
Post subject: 

ok here's my shot at a solution...
Java:

import java.util.*;
import java.math.*;

class test {
    final static BigInteger ONE = BigInteger.ONE;
    final static BigInteger ZERO = BigInteger.ZERO;
   
    public static void main(String[] args) {
        for (BigInteger i : primesTakeN(new BigInteger("10000")))
            System.out.println(i);
    }

    public static List<BigInteger> primesTakeN(BigInteger numPrimes) {
       
        List<BigInteger> primes = new LinkedList<BigInteger>();
        List<BigInteger> indices = new LinkedList<BigInteger>();
       
        for (BigInteger i = new BigInteger("2");
             i.compareTo(numPrimes) < 0; i = i.add(ONE)) {
            primes.add(i);
            indices.add(i);
        }
       
        Iterator<BigInteger> it = indices.iterator();
        while (it.hasNext()) {
            BigInteger m = it.next();
            Iterator<BigInteger> it2 = it;
            while(it2.hasNext()) {
                BigInteger n = it2.next();
                if (n.mod(m).equals(ZERO))
                    primes.remove(n);
            }
        }
       
        return primes;
    }
}
Notice i've used the BigInteger class... So it should work with arbitrary long numbers... either way.. this is not the "fastest" solution (especially cuz of the BigInteger class)... but.... gah!

Author:  HyperFlexed [ Sat Nov 05, 2005 6:23 pm ]
Post subject: 

I've never seen the purpose of computing primes. I sure hope I never get the project.


: